The idea of a virtual environment is precisely to provide an environment for your project, however redundant it may seem. That is, as long as you know how to set up an environment for your project, it doesn’t matter if you delete the environment you’re working in. In fact, you can even recreate this environment on another machine (for example on a remote server) that your project will work on.
However, keeping all your project’s settings (and possibly several others) in mind is somewhat problematic. You may forget some specific dependency and face enough difficulty to recreate your environment. To do so, it is common to save together with your project a list of the packages it depends on. You can create this list using Pip:
pip freeze > dependencias.txt
If you take a look at the generated file, you will see that, in addition to the package names, their respective versions are also included. Because of this, it is advisable that you include this file in your version control system.
With this, if you unintentionally delete your virtual environment, or need to run your project on another machine, just install the packages contained in this list. Again, Pip has a command for this. After creating and activating a new virtual environment, run:
pip install -r dependencias.txt
And that’s it, your project should work properly.
If your project is not active nothing will happen, but it is interesting you save in a file all the packages installed in this virtual environment, for this use
pip freeze > requirements.txt
– Paulo
I could start a new virtual environment from Requirements.txt?
– Renzo Rodrigues
After recreating the environment just use the command
pip install requirements.txt
and all packages required by the project will be installed.– Paulo
Missed the
-r
:pip install -r requirements.txt
– sergiopereira
Very good, man! It was worth too much.
– Renzo Rodrigues