I'm fairly new to python and I'm beginning to work with python virtual environments. When I create a virtual environment, I have to reinstall all the modules that I need for the current project that I'm working on. I'm wondering if the virtual environment somehow avoids redundancies in installation of modules if the same version of the module had already been installed in another project or on a system-wide level?
Also, would there be any point in installing modules on a system-wide level rather than just in virtual environments, since I would need to install the modules in the virtual environment anyway?
In a nutshell, Python virtual environments help decouple and isolate versions of Python and associated pip packages. This allows end-users to install and manage their own set of packages that are independent of those provided by the system.
A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.
One of your projects might require a different version of an external library than another one. If you have only one place to install packages, then you can't work with two different versions of the same library. This is one of the most common reasons for the recommendation to use a Python virtual environment.
Short Answer :
If you work with virtual environment, you need to install every dependecy (package) that you need for your project even if you installed this package in another virtual environment before.
That's exactly the purpose of virtual environment : each project has its own dependencies. That allows you to manage the dependencies clearly for each project without affecting others.
Of course you can install a dependecy (a package) globally by doing
pip install <Package name>
But before doing that be sure to not activate any virtual environment . This will install the package at the root installation of python which is the main environment.
But it is strongly not recommended to do that and working with virtual environment is always a good practice.
Extras :
Now just in addition to this answer you can use the command :
pip freeze > requirements.txt
This will create a file call requirements.txt
in your project's root folder.
This file looks like this :
numpy==1.18.1
pandas==0.25.3
In this example i installed in my virtual environment the package numpy
version 1.18.1 and the package pandas
in its version 0.25.3
In this way, if another project needs the package numpy in a newer or older version, i can manage this directly in its requirements.txt
without affecting other projects .
This file will also help you to re-install quickly and easily your dependencies of your environment (for example if you want to create another project with the same starting dependencies as your current project) by just doing :
pip install -r requirements.txt
Of course : be sure to first copy this requirements.txt file to your target's root folder of the new project and activate its virtual environment before doing that
Quick summary in commands :
1) Install virtual env (Linux):
pip3 install virtualenv
Install virtual env (Windows):
pip install virtualenv
2) Create a new virtual environment (linux and Windows ) :
virtualenv venv
Be sure to do cd
to your root project folder before doing that. This will create a new folder called "venv" (or whatever name you put after the virtualenv command but venv is a convention and a goo practice). If you are not in your root folder and for any reason you do not want to, you always can add a -p
flag to this command inn order to precise the path you want to install your virtual environment like so :
virtualenv -p /YOUR_PROJECT_PATH/ venv
3) activate the virtual environment (Linux) :
$ source YOUR_PROJECT_PATH/venv/bin/activate
If you call your virtual environment differently than venv be sure to replace venv by whatever you did call.
activate the virtual environment (Windows) :
C:\> YOUR_PROJECT_PATH\venv\Scripts\activate.bat
After this you should have this prompt :
Linux :
(venv) $
Windows :
(venv) C:\>
4) Install a package :
Linux (venv) $ pip3 install <package_name>
Windows (venv) C:\> pip install <package_name>
This command will install the <package_name>
only in the venv site-packages folder and this dependency will only be available for this virtual environment.
5) Freeze your dependencies :
Linux (venv) $ pip freeze > requirements.txt
Windows (venv) C:\> pip freeze > requirements.txt
This as said above will create the requirements.txt
in your project root folder (it will contains a list of all your packages name and their versions you installed in this virtual environment)
6) deactivate you virtual environment :
deactivate
If you create a new environment by doing the above step and you want this new environment to have the same dependencies as the first one :
cp YOUR_FIRST_PROJECT_PATH\requirements.txt YOUR_NEW_PROJECT_PATH
cd YOUR_NEW_PROJECT_PATH
Here Create and activate your new virtual environment (as explained above) then :
pip install requirements.txt
7) Install a package globally (not recommended) : if you have a current activated venv first :
deactivate
Then :
pip install <package_name>
This will install the package_name at the root installation of python.
For further comprehension of course :
https://docs.python.org/3/library/venv.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With