7

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

rbewoor
  • 305
  • 1
  • 3
  • 14
  • 1
    Try using a shebang. In unix a shebang is used to select the interpreter. It is the first line of your program and looks like this: `#!usr/bin/env python3` if you are using python 3.X – Filip Feb 05 '20 at 09:29
  • 1
    Does https://stackoverflow.com/a/1674444/2771562 answer your question? The python version specified has to be installed on your os. – Osvald Laurits Feb 05 '20 at 09:31
  • 1
    This exact scenerario is my main reason for using `pyenv` instead of other `venv` providers. With `pyenv` you can control the exact version for each env quite easily. I would check it out. – aviya.developer Feb 05 '20 at 09:34

4 Answers4

3

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).

sinoroc
  • 18,409
  • 2
  • 39
  • 70
2

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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
1

An alternative to that is to always specify the python version you wish use when running a script.

python3.6 test_script.py
1

Usually, when I'm on Linux and don't need a specific python3 version, I create native python3 environments.

python3 -m venv myenv
source myenv/bin/activate

But if I need a specific python3 version, I do:

python3.9 -m venv myenv
source myenv/bin/activate

To use a specific python3 version with native environments, you have to install that version using the native package manager (eg. apt).

Redowan Delowar
  • 1,580
  • 1
  • 14
  • 36