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

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

Related

Is Activate.ps1 required to add packages in venv? vscode

New to utilizing virtual environments. I understand to concept but not the complete execution within vscode.
I have read the vsCode guide on using Python venvs and the vscode guide on getting started with python
I'm not understanding the link between how the Terminal window displays its in the venv and how vscode shows its using the selected virtual interpreter. I want to make sure I know which one I am working in so that I can effetely manage packages.
These two images demonstrate my point. They both show me that vscode is using the .venv interpreter based on the blue region pointed by the red arrow. But the first image shows the Terminal is in the venv while the second image shows the Terminal is in the venv. This is confusing to me because when I use "pip" to install via the terminal I want to make sure its downloading the package to the right venv and not to global. I would assume if vscode is using the venv interpreter the Terminal should also default to the venv.
Do you have to just run the .\venv\Scripts\Activate.ps1 in the Terminal anytime you want to run "pip" to add a new package in your venv?
When the virtual environment name is displayed in front of your terminal directory, it means that your current terminal is using the activated virtual environment. At this time, directly using pip command will use the pip.exe in the virtual environment, and the installed package will also be installed in the virtual environment.
If the virtual environment is not activated, if your current terminal does not display the name of the virtual environment, directly using the pip command may use the pip.exe in the global environment, that is, the python version configured by the computer environment variable, and the package will also be installed under it.
If you're worried, you can use the full install command. For example, the following command uses the complete python interpreter path to ensure that the sklearn package is installed under the virtual environment .venv under folder py12.
e:\workspace\py12\.venv\Scripts\python.exe -m pip install sklearn
PS: If you choose a virtual environment interpreter, then creat a new terminal will automatically activate the environment. This is controlled by the setting below, which defaults to true.
"python.terminal.activateEnvironment": true,

Activate virtual environment and install requirements in the first setup automatically - Python, Windows

I want to write a first_setup.py script, where users can run it for the first time and the script will do the entire setup automatically, namely it will create a virtual environment, activate it, and install the requirements with pip. Users can then start to use the program without worrying about any dependency issue.
Therefore, I used venv together with subprocess library. My Python version is 3.7.5. With the following command, it creates the virtual environment in the working directory:
env_name = ".venv"
subprocess.run(["python", "-m", "venv", env_name])
However, activation doesn't work. I tried to activate it in various ways, for example:
subprocess.run([f"{venv_name}\\Scripts\\Activate.ps1"], shell=True)
This just opens Activate.ps1 in Windows Text Editor like a .txt file (?). I also thought to add .../Scripts/python.exe to the PATH variable, but it didn't work actually.
Furthermore, when the venv created by the script, VS Code pops up a message saying a venv detected, do you want to use it? If I click on Yes, then it changes my interpreter to venv, which is exactly what I want to do, but automatically with the first_setup.py script.
How can I proceed?
Does this first_setup script need to be Python?
The problem is that the activate script sets environment variables for the shell, which is why it’s usually run with ‘source’.
try using bash file:
python3 -m venv venv
source venv/bin/activate
pip install your_library

Powershell venv only use envirnmental variables python, not python in venv directory (Windows)

I use vevn with python 3.6.8, pip 18.1.
I set powershell executionPolicy RemoteSigned,
create virtual environment with venv, refed to venv official site : (https://docs.python.org/3.6/library/venv.html)
and also activated according to powershell, used .<venv>\Scripts\activate.ps1
But when I execute 'pip list' command, it shows all packages installed in global, not in venv.
And when execute python - import sys - sys.prefix, to check python directory,
I can find, powershell venv uses environmental variables python.
Of course, run it on cmd in same way, it works correctly.
pip list shows
The pip list shows only packages installed in venv, and sys.prefix is python in venv directory.
Does anyone know why it works like this?
Powershell works correctly like cmd
Maybe you activated the "--system-site-packages" switch by accident when installing the virtual env. This switch makes all python packages in the global system available to the virtual env.
Also check that after activating the environment your powershell indicates that you are actually using the virtual env. This is indicated by (env_name) at the beginning of the new power shell line.

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.

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