virtualenv enable the environment global - python

I am working in the environment when activate the ll_env
me at me in ~/desktop/django/learning_log
$ source ll_env/bin/activate
(ll_env)
me at me in ~/desktop/django/learning_log
$
When change to the parent directory, it still is in the scope of the virtual environment:
(ll_env)
me at me in ~/desktop/django
$
I assumed that ll_env might disappear when jump out of the directory where environment files lives
How Django enable the environment global?

It is not the present working directory that determines your environment. To jump out of the virtual environment, you need to deactivate it.
Using the command: deactivate

It may seem unintuitive at first, but it's important to understand that the current directory is not related to the active virtualenv. The active virtualenv determines where python should look for installed dependencies, and where it should install new dependencies to. It places that directory on your path, which is all that really matters in the context of working with a given virtualenv.
This means that you can cd anywhere on your system, do a pip install foo, and know that foo will be installed to a known location for the current venv, not to the directory you happen to be sitting in right now.

virtualenv and virtualenvwrapper give you access to a function called deactivate to stop using the virtual environment.
$ deactivate
It's different with Anaconda environment, You will deactivate it with two-word command:
$ source deactivate

Related

Simple Python venv manager with different py versions?

To be honest, up until recently I was hoping to avoid everything related to vurtualenvs. Yep, very naïve.
All of the code I write using python is to be deployed using containers, where "one container - one app" is totally true for now. And also number of CLIs being distributed also to my coworkers as wheels.
What I came up with is two "global" environments living in my homedir:
"default" virtual env with python 3.10 and some general packages installed. Using this venv I develop current versions of my applications and do my regular job. It is sourced automatically in .bashrc.
Virtual env with python 3.9. This is used to support legacy versions of/not yet updated applications in case if something should be done to them.
And also for some of the projects there are dedicated virtual envs living in a project folder needed only for this particular project, python version there may vary from 3.7 to 3.10.
As a consequence, I have to change current environment by manually sourcing the right activate script several times a day, and this isn't funny.
After some research on the topic, I found things like virtualenvwrapper, Pipenv and Poetry, but none of them seem to meet all my needs: "global" env to use across multiple projects and outside projects (because of CLIs); ability to create env with given python version; easy switch between virtual envs.
Is there any solution for this?
UPD: Some illustrations
Next virtual environments were created inside ~/pyenv/:
$ virtualenv -p /usr/local/bin/python3.9 default3.9
$ virtualenv -p /usr/local/bin/python3.10 default3.10
Next, .bashrc was updated to do source ~/pyenv/default3.10/bin/activate.
Now, for the vast majority of tasks it is enough: everything needed for day-to-day job is installed in default3.10 venv.
In case I need to switch to python3.9 the following is required:
$ deactivate
$ source ~/pyenv/default3.9/bin/activate
And if then I need to work on some project with its own venv, inside project folder I have to do:
$ deactivate
$ source env/bin/activate
Too many manual inputs. At minimal, I'd like to do something like $ workon default3.9 no matter of the current directory I'm in, and it should activate default3.9 venv correctly if no venv is active right now or if any venv if already active.
The same should work inside project folder: $ workon <local_venv_name> to switch to the venv living in current project folder.
And something like venv create <enterpreter path/py version> to create new "global" or "local" (in current folder only) venv, and venv list to get the list of available "global" and "local" environments.
to use the global environment inside the project's local virtual environment.
set
include-system-site-packages = true
in pyvenv.cfg file located in .venv (virtual environment installation directory) directory of each project.

Do I need to activate virtual environment when using venv

Using Python 3.7.0 on Mac. Trying to use venv module that was added post python 3.4.
I setup my virtual env using python3 venv -m path/to/my/dir - my question is do I need to activate this virtual env to use?
The documentation seem to imply I don't need to?
You don’t specifically need to activate an environment; activation just prepends the virtual environment’s binary directory to your path, so that “python” invokes the virtual environment’s Python interpreter and you can run installed scripts without having to use their full path. However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment’s Python automatically.
If I don't have to activate, what is the benefit of prepending venv to binary directory? Wouldn't this have to happen regardless for a venv to work?
Activating the virtualenv gives you convenience. It is never required.
Even for scripts that are configured to run with #!/usr/bin/env python, (which looks up the python executable on your path), you could manually update the PATH environment variable:
$ PATH="/path/to/venv/bin" some_script
Activating makes the PATH update stick until you deactivate again, and that can be convenient.
For example, I regularly have several virtualenvs in use at any one time. Some of them are there only to install some command-line tools I have symlinked into my ~/bin/ directory, another is home to a Jupyter notebook, and 2 more are used to quickly verify code snippets in different Python versions with access to 3rd-party libraries. I don't activate any of those.
When you don’t activate a virtualenv, all that happens is that your terminal PATH variable is not updated to put the bin directory of the virtualenv first, so when you enter python or pip or other script without any path into the terminal, the shell will find a different Python binary to run. You can always use any of the commands in the virtualenv bin/ directory by giving the full path to that command.

Activating python virtual environment does not switch to local versions of pip and python commands

I have created a virtual enviroment using virtualenv command with a standard procedure. Now, when I activate it or deactivate, the output of which pip or which python gives me same output /usr/local/bin/pip. However there is copies of python and pip commands in my vitrual enviroment directory - I have found them there. What might be the problem?
Did you move the virtual environment folder to a different name? The original path to the virtual environment is written into the generated activate script so if you move the environment activate will set your path to the old path of the virtual environment.
To fix this run virtualenv --relocatable $YOUR_VIRTUALENV_PATH then modify the VIRTUAL_ENV variable in the activate script to point to your new location.
I think it happen when I moved the environment folder to a different location.
I solved it by reinstalling virtualenv and creating a new environment

source /bin/activate activates the system interpreter not the venv one

When I run source bin/activate , the virtual environment seems to be activated since I can see (venv). However when I run which python , I realise its using the system wide interpreter i.e /usr/bin/python and therefore any modules in install are installed system wide. I created the virtual environment using Pycharm and when I my scripts through Pycharm, it works fine. But right now I need to run them through terminal and therefore I need the virtual environment.
To create virtualenv $ virtualenv env && source env/bin/activate then which python would show the right python here. You only need care while doing this. Sometimes you have to remove completely the env, close and restart the terminal and even source deactivate. Those are options. Virtualenv guide
EDIT:
There is no need to source deactivate. Deactivate is enough

activated virtual environment doesn't work

I have virtual env called env1
I'm activating it via: source env1/bin/activate.
It does seem to be activated however if I simply run:
python
I can't import any packages I have in my virtual env. This seems to be the case with every virtual environment I have on this machine.
What's wrong with my system? or am I doing something wrong myself?
Its hard to tell just by your description but:
Activated environment will (in most linux distros) prefix your shell prompt with the name of environment. Like:
username#computer ~ $ source env1/bin/activate
(env1)username#computer ~ $ python
That should run you python from env/bin/python
Be sure that you have installed all packages you want included. Virtual environment can also be set to include or omit system packages. It is easily set during creation of virtualenv by including --system-site-packages argument.
See: http://virtualenv.readthedocs.org/en/latest/virtualenv.html#the-system-site-packages-option

Categories