Reputation: 5238
I have installed Python 3.4.0 and created virtual environment with python -m venv myenv
. How can I change Python version in my virtual environment? Documentation says:
Each virtual environment has its own Python binary (allowing creation of environments with various Python versions) and can have its own independent set of installed Python packages in its site directories.
UPDATE
Please, note that I ask about venv from standard library, not about virtualenv. Let me provide some links.
I don't see something like a --python
flag in venv.
Are venv and virtualenv absolutely similar? Is venv is so unpopular and no one uses it so that virtualenv remains the standard?
Upvotes: 43
Views: 51683
Reputation: 9832
Homebrew can provide a specific Python vesrion for the basis of a venv
virtual environment.
brew install [email protected]
python3.11 -m venv my311_venv
cd my311_venv
source bin/activate
### prompt now shows (my311_venv)
python --version
# Python 3.11.9
python3 --version
# Python 3.11.9
which -a python
# …/my311_venv/bin/python
ls -l my311_venv/bin/python
# …/my311_venv/bin/python -> python3.11
ls -l my311_venv/bin/python3.11
# …/my311_venv/bin/python3.11 -> /opt/homebrew/opt/[email protected]/bin/python3.11
Upvotes: 0
Reputation: 5238
It's simply impossible. To create a Python venv of a specific Python version, we need this specific version.
Obviously, a Python interpreter doesn't "include" all the previous versions with their behavior. Python 3.4.1 cannot contain Python 2.7.8 executable anywhere inside.
Upvotes: 3
Reputation: 39193
As of 2021, you can't use multiple Python versions with the standard library venv
. It will use just the installed Python.
For alternatives you need other tools. You can use:
conda env export --from-history
).Upvotes: 0
Reputation: 13571
As another answer said, you need the specific version already there to create virtual env for it. So if you already have it somewhere in your system, you can do it. For example, OSX comes with Python2.7, and so to create a 2.7 virtual environment to avoid messing with the system one, do:
$ virtualenv -p /usr/local/opt/python@2/bin/python2.7 venv
Basically:
$ virtualenv -p <path/to/existing/python> <path/to/new/virtualenv/>
Upvotes: 1
Reputation: 53813
On Linux/Mac you can easily install multiple versions of Python next to the main one and you can use the venv package from the standard library to create virtual environments from each version >= 3.3.
Create venv
$ python3.3 -m venv myvenv_foo # Create a python3.4 venv named 'myvenv_foo'
$ python3.4 -m venv myvenv_bar # Create a python3.4 venv named 'myvenv_bar'
$ python3.5 -m venv myvenv_baz # Create a python3.5 venv named 'myvenv_baz'
# etc...
Activate venv
source myvenv_foo/bin/activate # Activates venv 'myvenv_foo'
Deactivate venv
deactivate
Notice: python
vs pythonX.X
If you have multiple Python versions installed, you can access each one by adding the version num to the command e.g. python3.5
, python3.6
, etc. But keep in mind that when you activate a venv, you bind it to the clean/versionless python
command, for as long as it's activated. E.g:
$ python -V # Use the *clean* 'python' command to show the main version of the OS.
Python 2.7.6
$ python3.5 -m venv myvenv_foo # Create a new venv from 'python3.5'.
$ source myvenv_foo/bin/activate # Activate venv.
$ python -V # The *clean* 'python' command is now bound to your activated venv.
Python 3.5.2
$ deactivate # Deactivate venv.
$ python -V # Now the *clean* command is bound back to the main version.
Python 2.7.6
Note
I suggest using Pipenv to create/handle virutal environments over the
venv
package.From the offical docs:
Managing multiple virtual environments directly can become tedious, so the dependency management tutorial introduces a higher level tool, Pipenv, that automatically manages a separate virtual environment for each project and application that you work on.
Upvotes: 34
Reputation: 37105
This is a very good question as there are several python modules / libraries (built-in & third party) with similar names and purposes. Can completely sympathise with OP's confusion.
There are really two different behaviours / responsibilities:
1). The ability to switch between different versions of (System) Python Interpreter eg. 2.7.10 or 3.5.0 etc
2). The ability to create virtual environments (which is just a local folder containing all the plumbing (binaries and libs) for a particular version of python. Can sort of think of this as a frozen local instance of a particular python version. Essentially it is a self-contained, light-weight python installation.
A module like pyvenv
provides 2) above. It will allow you to create a virtual environment that is set at the version of Python that was used to create it.
$ python --version
Python 3.5.0
$ pyvenv myenv # myenv is now a local environment using Python 3.5.0
For further infoormation on pyvenv, see library/venv
A module like pyenv
(the names are confusing, right? Notice, pyenv, and not pyvenv) on the other hand, controls which VERSION of python your system is basically running. This provides 1) above. So, when not running a particular virtual env via pyvenv etc, this is the "global" version in use. In fact, it is slightly more convoluted than that (as you can also setup local configuration etc), but essentially that is enough for this discussion.
For further information on pyenv see github.com/yyuu/pyenv
Suppose I want to run Python versions 2.7.10 and 3.5.0, then I would use pyenv to install these two versions (here, I chose as globals), and can view this using:
$ pyenv versions
system
* 2.7.10 (set by ~/.pyenv/version)
* 3.5.0 (set by ~/.pyenv/version)
$ python --version
Python 3.5.0
$ which python
~/.pyenv/shims/python
$ python2.7 --version
Python 2.7.10
Yes, there are several prominant alternatives to each of the above referenced modules / libs. Heated discussions on Reddit / SOF etc detailing and arguing which is best. Many of them do very similar things...
Upvotes: 6