Starting the environment with dependencies
In short, to create an isolated environment to work with a project with the dependencies defined, just follow the steps:
Creating the new environment:
virtualenv ENV
Access the environment directory:
cd ENV
Copy the project to the environment directory, including the file requirements.txt
;
Activate the environment:
bin/activate
Install the project dependencies:
pip install -r requirements.txt
Work with the project, with all dependencies installed in the isolated environment;
When finished, disable the environment:
deactivate
Below is a more detailed description of how the virtualenv
and the main commands to work with a project in an isolated environment.
Installation
Using PIP, installation of Virtualenv can be done with a simple command:
pip install virtualenv
If successfully installed, a similar message will appear:
Successfully installed virtualenv-15.1.0
Creating the environment
To create an isolated environment with the virtualenv
, just execute the command:
virtualenv ENV
Being ENV
the name of the environment to be created.
In Windows, the command is slightly changed to virtualenv.exe ENV
.
The result of this command will be a directory name ENV
in the current path, containing the following folders:
bin/
include/
lib/
In Windows, it will be:
Include/
Lib/
Scripts/
The directories lib
and include
store the libraries installed in the environment, while the directory bin
stores the executables that control the environment. In Windows, the directories Lib
and Include
are the equivalent of lib
and include
, respectively, while the directory Scripts
is equivalent to bin
. The libraries pip
and setuptools
will already be installed by default in the created environment.
Installing packages
To add the packages to the new environment, you need to install from the pip
of the environment and no longer of the original of the computer. The executable of the pip
of the environment is in the directory bin
, therefore, to install a package, Django, for example, it is necessary to do:
bin/pip install django
Or, on Windows:
Scripts/pip install django
Activating the environment
To avoid any confusion between the global environment, the computer itself, and the isolated environment created, it is possible to activate the environment through the command:
source bin/activate
Or, on Windows:
Scripts/activate.bat
This command changes the value of the environment variable $PATH
to the directory bin
or Scripts
, on Windows. This way, if the environment is enabled, to install a package, just run the default command:
pip install django
If everything is executed correctly, a message would appear saying that the Django package is already installed, because we install it through the Scripts/pip
.
It is important, when finishing the work in the isolated environment that was activated, that it is disabled so that the value of the environment variable is restored $PATH
. To do this, simply execute the command:
deactivate
Or, on Windows:
deactivate.bat
Dependencies of the project
With the isolated environment enabled, you can generate the dependency file from the command:
pip freeze > requirements.txt
The result of this command will be the file requirements.txt
with the description of the project dependencies. In this case:
Django==1.11.2
To install project dependencies in the isolated environment, simply run:
pip install -r requirements.txt
Sensational! thank you so much for the well-crafted reply!
– Wallace Maxters