Virtual environment and Packages - Letsprogram

Share:
The virtual environment is a good practice for coding applications. Python applications use packages and modules which are not from the standard library. Sometimes an application needs a specific version of the package or module to repair a bug. This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.x of a particular module but application B needs version 2.y, then the requirements are in conflict, and installing either version 1.x or 2.y will leave one application unable to run.

Here we use a virtual environment self-contained directory tree that contains a Python installation for a particular version of Python, plus several additional packages. each application, you can use a different virtual environment and make your work easier.

Create a Virtual Environment

If you are using Python3 then you will already have venv in your system otherwise you should install it by using the pip command in the shell

python -m pip install  virtualenvwrapper-win

Create a directory where you want to work with

mkdir  foldername

create a virtual environment there by running venv module as a script with the directory module.

mkvirtualenv  name

This will create a virtual environment with a name as 'name'. After creating the environment you can work specifically on your application. 

Virtual environment and packages

Note: When you restart your shell your virtual environment will be closed you have to restart it with the respective command to again start your virtual environment.


Manage packages using pip

You can install, upgrade, and also remove the packages using pip command. By default, pip will install the packages from the python index. pip can also be used to search the packages from the python index, it will give you package name and version with it. 

pip has many commands like install, uninstall, freeze, search, etc.(Go through the installing pip for more information)

pip install django
pip install django == 3.0.6
pip install --upgrade django

If the package is already installed in your system it shows that requirements are satisfied and you can update the version also.
pip uninstall followed by the package name will uninstall that package.
pip list will tell you the number of packages in the virtual environment.
pip show followed by the package name will tell you the features of the package.
pip show Django
Name: Django
Version: 3.0.6
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: c:\users\balus\envs\django\lib\site-packages
Requires: sqlparse, pytz, asgiref
Required-by: django-urls

pip has many more options you can go through installing pip and get more info. You can also create a package.

No comments

F