Use of virtualenv python

Asked

Viewed 158 times

2

I’m starting to use virtualenv for projects and in the process I came up with a question, after activating the environment, every time I enter or exit the folder it activates and deactivates automatically, or I need to give activator to use it and deactivate to return to global configuration?

3 answers

4

In a simplified form, the activate will modify the environment variables during the terminal session.

[...]

VIRTUAL_ENV="/home/user/python/venv"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

[...]

Code snippet from archive venv/bin/activate

That is, when you activate the virtual environment an environment variable VIRTUAL_ENV is created with the path to where the environment was generated; soon after it modifies the value of the variable PATH adding the newly created variable as the first path in the PATH search.

This way, when you run some command with the virtual environment enabled, the first directory to be analyzed in search of the executable of this command will be the directory venv/bin (in Windows will be venv/Scripts).

When performing the function deactivate the variables will return to the initial value, that is, the variable VIRTUAL_ENV shall be excluded and the PATH will return to value _OLD_VIRTUAL_PATH, created during the activate with the original value of PATH.

When you log out of your terminal, these environment variables will be removed, so there is no obligation for you to always call deactivate. You need to run it only when you need to quit the virtual environment while maintaining the same terminal session - common when you are, for example, connected via SSH to the server.

Navigating between folders during the same terminal session will not cause your virtual environment to be disabled.

1

Yes, when working on your project you will need to turn virtualenv on, and when you are done you need to turn it off.

For Windows, it is necessary to install the virtualenv module

  > pip install virtualenv

Then you need to create a Venv folder at the project root

> python -m venv "venv_name"

To activate the Venv:

> venv_name\Scripts\activate.bat

To exit Venv, type:

> deactivate

1


Hello .

About your question, yes you will have to turn on and off.

But you can delegate this function to the operating system in use if you have only one environment however if you are using environments with different versions will have to be manually.

using the following syntax :

ACTIVATE - (Gnu/Linux)

workon DEV38 (Python usage 3.8+)

workon DEV27 ( Python Usage 2.7+)

DEACTIVATE - (Gnu/Linux)

deactivate DEV38

deactivate DEV27

  • 1

    Jeferson, the question is about virtualenv, and your answer talks about virtualwrapper. From what I’ve researched, neither the module venv of Python3 and the library virtualenv have the command workon added to the PATH.

  • virtualenv is a creator of virtual environments, virtualwrapper manages. Using virtualenv only will have to enable and disable. Both are manually initialized. However if you always use a version you can specify. The examples are just a mention of the act of having to activate and deactivate. deactivate or even list lsvirtualenv. The part I explained is equivalent, the example was to enable and disable.

Browser other questions tagged

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