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:
Create the new environment:
virtualenv ENV
Access the environment directory:
cd ENV
Copy the project to the environment directory, including the file requirements.txt
;
Enable the environment:
bin/activate
Install project dependencies:
pip install -r requirements.txt
Work with the project, with all dependencies installed in the isolated environment;
When you finish, disable the environment:
deactivate
Below is a more detailed description of how virtualenv
works and the main commands for working with a project in an isolated environment.
Installation
Using PIP, VirtualEnv installation can be done with a simple command:
pip install virtualenv
If installed successfully, a similar message will appear:
Successfully installed virtualenv-15.1.0
CreatingtheEnvironment
Tocreateanisolatedenvironmentwithvirtualenv
,justrunthecommand:
virtualenvENV
BeingENV
thenameoftheenvironmenttobecreated.
OnWindows,thecommandisslightlychangedtovirtualenv.exeENV
.
TheresultofthiscommandwillbeadirectorynamedENV
inthecurrentpath,containingthefollowingfolders:
bin/
include/
lib/
In Windows, it will be:
Include/
Lib/
Scripts/
The lib
and include
directories will store the libraries installed in the environment, while the bin
directory stores the executables that control the environment. On Windows, the Lib
and Include
directories are the equivalent of lib
and include
, respectively, while the Scripts
directory is equivalent to bin
. The pip
and setuptools
libraries will already be installed by default in the created environment.
InstallingPackages
Toaddthepackagestothenewenvironment,youmustinstallfrompip
oftheenvironmentandnomorethantheoriginalofthecomputer.Thepip
executableoftheenvironmentisinthebin
directory,sotoinstallapackage,Django,forexample,needstobedone:
bin/pip install django
Or, in Windows:
Scripts/pip install django
Activatingtheenvironment
Toavoidanyconfusionbetweentheglobalenvironment,thecomputeritself,andtheisolatedenvironmentcreated,youcanenabletheenvironmentbyusingthecommand:
source bin/activate
Or, in Windows:
Scripts/activate.bat
This command changes the value of the environment variable $PATH
to the bin
or Scripts
directory in Windows. So, if the environment is enabled, to install a package, just run the default command:
pip install django
If you run everything correctly, a message will appear saying that the Django package is already installed, because we installed it through Scripts/pip
.
It is important, when you finish working in the isolated environment that has been activated, that it is disabled so that the value of the environment variable $PATH
is restored. To do this, just run the command:
deactivate
Or, in Windows:
deactivate.bat
Project dependencies
With the isolated environment enabled, you can generate the dependency file from the command:
pip freeze > requirements.txt
Theresultofthiscommandwillbetherequirements.txt
filewiththedescriptionoftheprojectdependencies.Inthiscase:
Django==1.11.2
To install the dependencies of a project in the isolated environment, simply execute:
pip install -r requirements.txt