Python Virtual Environment | Python Packages
While coding in Python, we need to install different packages and modules based on our application. Do you know we can create virtual environments for handling the packages installed?
In this article, we will learn about virtual environments and their creation. Then we will see installing packages using Pip. We will also discuss some other operations we can do in pip to get information about the packages.
Let us start with the introduction to the virtual environment.
What is Python Virtual Environment?
When we are developing some application or a project in Python, there is a need to install the required packages and modules. For example, when we are doing some mathematical operation then we might install SciPy or NumPy.
There are circumstances when we need to install a particular version of the package. But when we install a new version of an existing package, then the old one gets deactivated automatically. What if we want both versions for different applications?
This is the situation when the virtual environment comes into play. It is a self contained tree containing installations of the particular Python version and other additional installed packages. To solve the above problem, we can create separate virtual environments for the applications. The different versions of the package can stay isolated in their own virtual environments.
Now let us see how to create a virtual environment in Python.
How to create a virtual environment in Python
To create a virtual environment, we use a module named venv. Yes, a module to create an environment that handles other modules! It installs the latest version of Python. Now to create the virtual environment, we need to open the command prompt write the below command.
python -m venv env_name
We can change the directory where we want to work using the ‘cd’ command and then specifying the preferred location. Now, this will create a directory with the name specified as the env_name in the specified location. This file contains a copy of the Python interpreter, standard library, and different supporting files.
Now to activate this environment, we give the below command.
env_name\Scripts\activate.bat
After doing this step, the shell will show the name of the virtual environment you are dealing with along with the current directory on the left hand side in the prompt.
Now let us go forward with the second important part, that is, the packages.
Using PiP for Python Packages
Pip is widely used for installing Python packages and modules. It automatically gets installed when you install Python from python.org. If we want we can install it manually too. It installs the packages from the Python Index (PyPI). Besides installing, we can use it for other purposes too. Let us see them in this section.
1. Installing Packages in Python:
The command for installing the package is simple and it is
pip install package_name
Example of installing numpy:
pip install numpy
Output:
Downloading numpy-1.21.2-cp38-cp38-win_amd64.whl (14.0 MB)
|████████████████████████████████| 14.0 MB 85 kB/s
Installing collected packages: numpy
Successfully installed numpy-1.21.2
After installing, if we go to the library in the virtual environment directory and check, then we can see the numpy module file. In addition, we can also download a specific version of the module using the below command.
pip install package_name==version
We can also search the version of the installed package. The below codes give the example.
Example of finding the version:
import numpy numpy.version.version
Output:
Example of finding the version in Python:
numpy.__version__
Output:
2. Search Feature:
We can search for a particular keyword using the ‘search command. And we get all the packages related to that keyword. We can do this either by giving the command
pip search keyword
Or by installing pip-search using pip and then using pip_search
pip install pip-search
pip_search keyword
On searching for the image, we get the following result.
Example of search in PiP:
pip_search image
Output:
3. Learning about a particular package:
We can use the show command to know about any package.
Example of learning about the package:
pip show numpy
Output:
Version: 1.21.2
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: c:\users\…\lib\site-packages
Requires:
Required-by: tifffile, tables, statsmodels, seaborn, scipy, scikit-learn, scikit-image, PyWavelets, patsy, pandas, opencv-contrib-python, numexpr, numba, mkl-random, mkl-fft, matplotlib, imageio, h5py, caer, Bottleneck, bokeh, bm3d, bkcharts, astropy
4. List of packages installed in Python:
We can find the list of packages using the command ‘pip list’.
Example of listing all the installed packages:
pip list
Output:
———————————- ——————-
alabaster 0.7.12
.
.
.
numpy 1.21.2
.
.
.
pip 20.2.4
.
.
.
zope.interface 5.1.2
All the packages present will be listed in ascending order. The output shows the first, last, pip, and numpy packages with the dots represent various packages installed based on the user.
5. Checking for Updates:
We can check for updates using the upgrade command.
Example of checking updates in the package:
pip install -upgrade numpy
Output:
c:\users\…\lib\site-packages
6. Uninstalling Packages in Python:
To uninstall a package, we use the ‘uninstall command. Let us see an example of uninstalling the numpy package.
Example of uninstalling the package:
pip uninstall numpy
Output:
Uninstalling numpy-1.21.2:
Would remove:
c:\users\…\lib\site-packages\numpy-1.19.2.dist-info\*
c:\users\…\lib\site-packages\numpy\*
c:\users\…\scripts\f2py-script.py
c:\users\…\scripts\f2py.exe
Proceed (y/n)? y
Successfully uninstalled numpy-1.21.2
7. Freeze:
This command also lists the packages installed, but in a different format.
Example of freeze:
pip freeze
Output:
.
.
.
numpy==1.21.2
.
.
.
zope.interface @ file:///C:/ci/zope.interface_1602002494740/work
Conclusion
We discussed the virtual environment, and it’s working. Then we learned packages and the pip used to handle them. Hope the concepts covered are clear. Happy coding with Python!