Install with PIP through the Resets.txt file inside Virtualenv?

Asked

Viewed 4,863 times

7

I learned these days that through command pip freeze > requirements.txt, i can generate a list of dependencies of a specific project where I am using VirtualEnv.

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

3 answers

8


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:

  1. Creating the new environment:

    virtualenv ENV
    
  2. Access the environment directory:

    cd ENV
    
  3. Copy the project to the environment directory, including the file requirements.txt;

  4. Activate the environment:

    bin/activate
    
  5. Install the project dependencies:

    pip install -r requirements.txt
    
  6. Work with the project, with all dependencies installed in the isolated environment;

  7. 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

inserir a descrição da imagem aqui

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.

inserir a descrição da imagem aqui

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

inserir a descrição da imagem aqui

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

inserir a descrição da imagem aqui

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!

2

The command is pip install -r requirements.txt.

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

  • 2

    If you think your answer will be better, I would recommend at least addressing the difference between running the pip and the isolated environment. This information is fundamental to answer the question.

  • Your answer is much better, more complete. But I have only tried to give another alternative that goes straight to what was asked, which can (and was) answered in 3 lines. As for addressing the difference between global Pip and Pip within virtualenv, this was not asked, so I did not write.

  • 2

    The title of the question specifies that the installation should take place in an isolated environment created by virtualenv.

  • Your answer was the same as I answered (including I was the first to reply), the author told me that this is only part of the answer, the environment part is very important, I had deleted the answer, but I restored it just so you could see the https comments://en.stackoverflow.com/a/209388/3635

  • @In the author’s comments he says "In fact, Virtualenv is to have an isolated environment, where you don’t need a global Python, but only what you need. This helps to control things. - Wallace Maxters" IE, he knows what serves virtualenv and as he himself said in the question, he is using virtualenv, IE, he knows how to activate it. If he knows what it is for and knows how to activate it, all that remains is to know how to install the libraries right? (that was the only thing asked). And that’s exactly what I wrote. Enter the environment and execute the command.

  • @To conclude, you William answered what the author asked. I answered what he asked and a little more. Anderson answered what I answered and more.

  • @gabrielbelini might be, is that I talked to Wallace on chat and I had the impression that something was missing, let’s hope he (author) confirm, you may be right :)

Show 2 more comments

2

In accordance with the documentation I believe it’s something like:

pip install -r requirements.txt

I just don’t understand VirtualEnv, so I cannot say whether something more is needed.

  • That explains the question there.

  • @Wallacemaxters sorry I didn’t understand

  • It is that when you activate the virtual env, you can run the commands as if it were in the global cmd

  • @Wallacemaxters I think I understand, virtualenv is to have "several python" on the same machine

  • In fact, Virtualenv is to have an isolated environment, where you don’t need a global Python, but only what you need. It helps to control things.

  • @Wallacemaxters so I believe my answer is wrong or only partially correct, lack the question of virtualenv even right? I will install today and update. Thanks for the opportunity to let me reply, I know you should already know the answer xD

  • Not exactly.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.