Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the Python interpreter in virtual environment (Ubuntu 18.04LTS)?

I always used Anaconda on Windows so far and could set up an environment while choosing which exact Python to use. E.g. conda create -n myEnvName python=3.7

Now, I want to familiarize with Ubuntu 18.04 LTS and use basic Python environments.

So I followed these steps:

  1. Created folder in my home = ~/.venvPython
    • (a) I think I already had a 2.7 and 3.6 by default on the OS.
    • (b) I do not remember for sure, I think I had to do this sudo apt-get install python3-venv.
  2. Created environment this way after CD'ing to .venvPython folder ran this: python3 -m venv venv1BigDataPgm2
  3. source ~/.venvPython/venv1BigDataPgm2/bin/activate
  4. Command python --version says: Python 3.6.9

Running whereis Python shows this:

rohit@rohitUb18043LTS:~$ whereis python
python: /usr/bin/python3.6 /usr/bin/python3.6-config /usr/bin/python2.7-config /usr/bin/python3.6m-config /usr/bin/python /usr/bin/python3.6m /usr/bin/python2.7 /usr/lib/python3.8 /usr/lib/python3.7 /usr/lib/python3.6 /usr/lib/python2.7 /etc/python3.6 /etc/python /etc/python2.7 /usr/local/lib/python3.6 /usr/local/lib/python2.7 /usr/include/python3.6 /usr/include/python3.6m /usr/include/python2.7 /usr/share/python /usr/share/man/man1/python.1.gz

My doubts: Can I specify a Python version directly while creating the environment like with conda? How do I change this to some other interpreter instead of the 3.6.9? Do I have to manually install a different Python first, then point it somehow?

Please guide me. Thank you. Rohit

like image 328
rbewoor Avatar asked Feb 05 '20 09:02

rbewoor


People also ask

Can I change the Python interpreter version in an existing virtualenv?

This is a short article describing how you can change the Python interpreter version in an existing virtualenv. If you start working on a project and realize you wish you started with Python 2, or you started with version 2 and wish you started with version 3, this can easily be change.

What version of Python is installed on Ubuntu 18 04?

Create Virtual Environment for Python 3. Ubuntu 18.04 ships with Python 3.6 by default. You can verify that Python 3 is installed on your system by running: python3 -V. The output should look like this: Python 3.6.5. If you want to install the latest major release of the Python language, Python 3.7 follow this instructions.

How do I change the Python interpreter in Ubuntu terminal?

From the popup context menu select Edit Configurations. A new Run/Debug Configurations window will appear. On the right side of the Python interpreter field is a dropdown triangle that when clicked brings up a context menu that allows you to change the Python interpreter.

How do I create a virtual environment in Python 3?

To create a virtual environment in Python3 and activate it immediately use this command in your terminal: To deactivate the environment use the deactivate command. To list all available virtual environments use the command workon or lsvirtualenv (lsvirtualenv will show the same result as workon but in a fancier way) in your terminal:


2 Answers

As far as I can tell the venv standard library appeared in Python 3.3 and was never backported to 2.7.

venv can only create virtual environment for its own version of the interpreter and the virtual environment directory can not be moved to a different location or be renamed. Python 3.foo can not create a virtual environment for Python 3.bar. So it is best to pick the wanted interpreter right from the start.

Since, as shown by the output of whereis python, you seem to already have multiple Python interpreters already installed, you should be able to do something like the following:

$ /path/to/python3.3 -m venv /path/to/my/venvs/venv33
$ /path/to/python3.8 -m venv /path/to/my/venvs/venv38

There seems to be a way to change the Python interpreter associated with a virtual environment (I have not tested it, not sure what the limitations are):

$ /path/to/python3.8 -m venv --upgrade /path/to/my/venvs/venv33

Alternatively use virtualenv which seems to offer a bit more flexibility, but is probably less efficient (its next major release, virtualenv 20, should bring a lot of improvements though).

like image 194
sinoroc Avatar answered Sep 23 '22 08:09

sinoroc


Ubuntu and other Debian-based systems generally ship whichever Python version was current and deemed sufficiently tested when the release was published; after that, only security updates which preserve the version number but add patches are released (so you might get 3.6.9-123security4 instead of 3.6.9-5 or whatever was current when the release was cut).

If you want to run a specific Python version on one of these platforms, see if you can find an Apt source which provides this version for your system (Ubuntu has a soft underbelly of unofficial PPAs of various repute; Debian has backports) or install it from source yourself. There are add-ons like pyenv which let you do this rather easily, safely, and transparently.

There may also be an existing package which gives you a particular newer version; for example, you can do apt install python3.7 and apt install python3.8 on Ubuntu 18.04, but there are no packages for 3.5 or 3.9. Try apt policy python3.7 to see which specific minor version is available from the Ubuntu package archive.

like image 32
tripleee Avatar answered Sep 22 '22 08:09

tripleee