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 f
No it does not but that is the main point of having virtual environments because libraries depend on another and thus upgrading or modifying a library in one environment does not affect other environments.
Virtual environments segregate modules for an application. This means that upgrading modules in one application does not impact another application. Since modules occasionally have breaking changes, this can be important.
Are you using the requirements.txt file to help manage the dependencies? It makes installing them easier when you need to reset a 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
Virtual environments are semi-isolated Python environments (self-contained directory) that allows packages to be installed for use by a particular application, rather that being installed system wide.
The main advantage being that it enables the user to install for each python project/environment different libraries, dependencies and why not, even a different Python interpreter.
Please take a look here: https://docs.python-guide.org/dev/virtualenvs/ it will explain more thoroughly