wtfzambo
wtfzambo

Reputation: 628

How to choose which python executable to run when using venv?

I have a python installation on my pc (windows 10) which comes from Anaconda. I am a data scientist and using conda as a package manager is very convenient for me.

However, sometimes I want to develop a small app or script to share with my colleagues. In these cases I create a project folder and python -m venv .venv inside it. This way, I can install only the essential packages I need, and later share the requirements.txt file.

The issue I'm having, is that the python interpreter being used is still the default one, namely, the one that came with Anaconda, even if I activate the virtual environment and deactivate the conda one.

Specifically, if I run python in the terminal, I get this warning message:

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

This is rather inconvenient. My base python installation is 3.7, but if I wanted to use an earlier version, or 3.8, I can't seem to be able to choose.

I would expect that the python executable being used is the one in the current active environment, but this doesn't seem to be the case.

How can I obtain that?

Upvotes: 1

Views: 4890

Answers (1)

rolf82
rolf82

Reputation: 1571

First you have to install the version of python you want to use in your venv. It must already be available somewhere on your system to create a venv using it.

Then instead of just python -m venv .venv you specify which python with the full path : /path/to/pythonX.Y -m venv .venv

You can't have a venv that shares multiple versions of python, to my knowledge at least.

Upvotes: 1

Related Questions