'How to choose executable which runs a virtual environment
How does one create virtual environment that runs on it's own executable? I used anaconda to create new Venv (to experiment with installing new modules) by cloning some old Venv
conda create --prefix ./new_env --clone C:\full_path_to_old_env\old_env
But new Venv is still using the "python.exe" from old Venv, as can be seen by calling
import sys print(sys.executable)
Because of that, when I install new modules, using for example
pip install selenium
inside the new Venv, it gets installed in the folder of new Venv (as expected) and is unreachable for old executable.
I saw similar question addressed here Changing Python Executable by changing where to install new modules, but that defeats the idea of independent environments.
The only way to for Venv to run it's own copy of python I found is using
sys.executable = r'C:\full_path_to_new_venv\python.exe'
at the beginning of the notebook, but this seems more like forced patch, rather than solution. This also gets overridden with every initiation of new Venv, so all notebooks running in new Venv would need it
Solution 1:[1]
I found what was wrong: I was using Jupyter Notebook to run python virtual environments. But I did not install ipython kernel to new VEnv
Apparently, in such case Jupyter doesn't throw an error when a kernel exists somewhere else and just runs the kernel it found.
Hence, the fix is to install ipython kernel to new VEnv:
$ipython kernel install --name .venv --user
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ivan Panfilov |