Unable to activate Python Virtual Env on compute in VS code - visual-studio-code

I have two computers, both running VS Code.
I was able to get the virtual environment running on one computer without problem running
python -m venv virtual_env
virtual_env\Scripts\activate
This machine only has Python3 installed and I ran that in a Python terminal in VS code.
On the second machine, it has both Python 3 and Python2 installed. I have tried to set everything up identically between them, but when I run the same code, it fails.
When I run
python -m venv virtual_env
it returns either "No module named venv" (and it's running that in Python 2.7 despite the interpreter being set to 3.8)
When I run
python3 -m venv virtual_env
it says "The system cannot find the file specified".
The directory does appear in the explorer of Machine #2, but in the "Scripts" directory, there does not exist an "activate" file (like there is in Machine #1).
Not sure how to continue troubleshooting this.

Related

Python interpreter in venv cannot be selected because it is not an .exe file

Python is installed in my virtual environment
But, in VScode when I go to the folder venv/bin it can't be selected as an interpreter, because it is somehow not a .exe file. (It is also 0kb)
Environment:
I am using ubuntu linux. Python runs in the command line, but I can't run it in the 'Run python file' in VS code.

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.

Setting up jupyter kernel in remote VSCode notebook

I am trying to run some notebooks in my virtual environment in the VSCode (remotely connected). I install the venv as usual via python3 -m venv <venv-name>, activate it and install all the needed modules. When I run which ipython I get the one from the venv so I install the kernel via ipython kernel install --name "<name>" --user and it is successfully created in ~/.local/share/jupyter/kernels/ directory and the kernel.json points to the venv python. Then I open the VSCode and select both the Python: Select Interpreter and Jupyter: Select Interpreter to start Jupyter server to point to the virtual environment's python, sth. like .../<venv-name>/bin/python3.
However, when I try to run the cell it wants me to select kernel (I can also do it myself in the upper right corner of the VSCode), but my newly created kernel is not there. There are only two (same) ones from usr/bin/python.
It is really strange since twice in two days my kernel magically appeared for one notebook and worked as desired, but when I opened a new notebook, my kernel was gone again. I tried to remove/reinstall kernels, venvs, VSCode's Python and Jupyter extensions but nothing helped. Any suggestions?
For now, I start the kernel in remote command-line via jupyter notebook --no-browser --ip=<ip> and then insert the connection link to Jupyter Server in the bottom right corner of the VSCode status bar but am wondering if there is an easier way since all the stuff (except VSCode) is on a remote machine?
This way is not easy. You can set up Jupyter Kernel easily.
Firstly, using ssh to connect to the remote server.
Secondly, open Command Palette (⇧⌘P) and enter Python: Select Interpreter, you can directly connecting to remote kernel.
resource: https://code.visualstudio.com/docs/datascience/jupyter-notebooks

Python virtual environments do nothing

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.

Multiple .bash_profiles on mac osx

I'm taking a course on mongodb where I have to install the mongo shell on my mac. In order to run the mongo shell, I had to change the PATH in .bash_profile. The thing is I had anaconda installed and the bash profile for conda (which I overwrote for the mongo shell). So now, my conda prompt is not usable. I created a local conda environment where I can install some python dependencies I need, but for some reason, I can't execute conda commands in the folder I created the local environment for. I keep getting a 'command not found' response. I know for sure the conda environment was activated because its listed in my anaconda navigator.
Any ideas?