Installing with PIP through the requirements.txt file inside VirtualEnv?

6

I've learned these days that through the pip freeze > requirements.txt command, I can generate a dependency list for a specific project where I'm using VirtualEnv .

How do I install all the libraries contained within this requirements.txt file, which I exported through the command pip ?

    
asked by anonymous 01.06.2017 / 17:16

3 answers

7

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

    BeingENVthenameoftheenvironmenttobecreated.

      

    OnWindows,thecommandisslightlychangedtovirtualenv.exeENV.

    TheresultofthiscommandwillbeadirectorynamedENVinthecurrentpath,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,youmustinstallfrompipoftheenvironmentandnomorethantheoriginalofthecomputer.Thepipexecutableoftheenvironmentisinthebindirectory,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.txtfilewiththedescriptionoftheprojectdependencies.Inthiscase:

    Django==1.11.2

    To install the dependencies of a project in the isolated environment, simply execute:

    pip install -r requirements.txt
    
        
    01.06.2017 / 22:50
    2

    According to documentation I think it's something like:

    pip install -r requirements.txt
    

    I just do not understand VirtualEnv , so I can not say if something else is needed.

        
    01.06.2017 / 17:26
    1

    The command is pip install -r requirements.txt .

    Just run this within the desired environment and all the libraries contained in requirements.txt will be installed.

        
    02.06.2017 / 01:13