Python virtual environments do nothing - virtualenv

I've recently tried to start using virtual environments but I cannot get them to work as they should.
When I activate a virtual environment, my python interpreter seems to ignore it completely, and continue using my global environment. It says that the packages that I've installed to my virtual environment do not exist and it continues to use my global packages. I've tried using virtualenv and venv and they both have the same problem. Am I supposed to have my project files in a certain location with respect to the virtual environment folder?
FYI, I am using Windows 10

My virtual environment was also using my global environment only (using system site programs and packages; Windows 10). I used commands virtualenv .env -p python3 and .env\Scripts\activate. I found out there is a problem with paths (%PATH%). VEnv was in echo %PATH% but when I tried where python or where pip, path for VEnv was not there. I had very long length and diacritics in my path. I moved the project to a new directory (short simple path), generated a new virtual environment, activated and eureka. Virtual environment works.

Related

VS Code cannot find virtual environment on the interpreter, but can find it on integrated terminal

I have a fully working virtual environment installed on my Linux machine.
This venv can be regularly used by the terminal in VS code calling source /mypath/venv/bin/activate.
The problem is that the Python interpreter in VS code cannot access any of the packages in the virtual environment, despite setting up the path on the interpreter as described in most of the guides.
I decided to manually set up the path in the settings.json file inside the .vscode folder as follows:
{
"python.pythonPath": "/mypath/venv/bin/python3.8"
}
venv is still not accessible through the interpreter. Any other suggestions?
I will answer my own question.
Turned out my pip installation was pointing to a path (standard /home/username/.local/bin/pip) which was different from my venv directory (/my_path/venv/bin/pip).
You can display the path by executing the command which pip.
In my very specific case, there was some mix-up when I first setup my Linux machine, meaning that venv only had a small amount of packages installed, while the directory containing the Python libraries and actually being used was the pip path. In other words, activating venv did not make any difference, since the Python libraries where loaded from the pip path.
So, first I had to ensure that pip had to point to the my venv folder, by modifying the .bashrc file in /home/username/, replacing
export PYTHONPATH=/home/username/.local/lib/python3.8
export PATH=/home/username/.local/bin:$PATH
with
export PYTHONPATH=/my_path/venv/lib/python3.8/
export PATH=/my_path/venv/bin:$PATH
All I had to do after was re-installing each of the required packages in the newer venv (generating a requirements.txt file from the older pip path helped).
Then I selected the venv path in the VS Code interpreter and everything is working fine now.
You actually do not need the settings.json file.
You could try to remove the .venv folder and create a new one by
python -m venv .env
It seems that vs code have changed something from .venv to .env. I'm not sure why.
After doing python -m venv .env open the terminal in vs code and it will active your .env.
You could (if you froze your pip installations) do a pip install -r requirements.txt and you are all good to go.
The default for the "python.envFile" setting is "${workspaceFolder}/.env" change it to "${workspaceFolder}/.venv" and restart vscode.

PowerShell Opening File instead of Running When in Python Virtual Environment

I am using Python 3.9.9 (installed via MSYS2) and Windows PowerShell on Windows 10 64-bit. I am trying to learn more about Python's virtual environment.
I was able to initialize and activate virtual environment using the respective commands in PowerShell.
python3 -m venv .venv
.\.venv\bin\Activate.ps1
I notice that before activating the virtual environment, python --version would work as expected and output the Python version installed. However, once virtual environment has been activated, the same command would prompt Windows to open a file (window below would pop up).
Window's reaction when in virtual environment
I know the virtual environment was activated because I could see (.venv) being displayed.
Is this expected behavior? What should I do for python commands to still work in virtual environments? Thank you for your help!

How is the PATH variable defined for the vscode process itself (not the integrated terminal)?

I'm currently using a vscode over a remote ssh connection and cannot figure out how to set the search PATH for the vscode process itself. I have set the PATH for processes run in the terminal in my .bashrc file, which is also sourced from .bash_profile.
In spite of this, vscode complains that pipenv is not on the path although it is visible to my integrated terminal session. In my .bashrc I am loading environment modules to load versions of needed libraries, which get put on the PATH. Since I created my virtualenv using pipenv in the terminal, it knows which python version to use and makes a link to it in the environment definition. Because of the way python virtual environments work, the actual python binary is copied to the virtual environment. And because vscode has hardcoded paths where to look for virtual environments it is able to find the correct version of python that is in use (despite it not seeing it in the PATH).
In addition, hardcoding the path to pipenv using the extension setting python.pipenvPath still produces a "not found" error.
Solutions that I have seen elsewhere suggest launching vscode from the command line so that the process inherits the PATH settings. However, this will not work over a remote connection.

How to get the list of the python virtual environments installed on a linux server

I am new to linux environments. I have a linux server on which I have installed python3 in some virtual environment (but I no longer remember the name of the virtual environment). I know that if I were using anaconda on a windows computer, I would do the following to get the virtual environment names.
conda info --envs
The question is how do I get the list of all the virtual environments on my linux server and the path to those environments? In particular, I want a link to the bin folder containing my python3 so that I can activate my virtual environment.
Assuming you have created the virtual environments with the python pip install virtualenv command, run the command
$locate activate
It will list down all the folders having 'activate' in it. Among those you can find the virtual environment folders you created. Remember, the virtual env folders are of this form your_env_name/bin/activate under the parent folder where it was created.

No module named inside virtualenv

I'm using Python virtualenv and I'm having a problem with a module (installed inside the virtualenv).
First of all, I activate the virtualenv:
source path_to_virtualenv/bin/activate
The virtualenv is correctly activated (its name appears in the shell). Inside that virtualenv, I installed mininet (a network simulator): I'm sure it is correctly installed (it is listed by the command pip list).
However, when I try to run my application, I obtain the following error over a module of Mininet API:
from mininet.net import Mininet
ImportError: No module named net
How is it possible? Using an IDE, it correctly detects all Mininet's modules (in the same virtualenv); does someone have some ideas?
Thanks
Check to see if your project is in the same directory as your virtual environment.
If not start your virtual env. in the the command prompt and then cd to the project.
hope that helps a little