%%HTML
<blockquote class="reddit-card" data-card-created="1582892002"><a href="https://www.reddit.com/r/ProgrammerHumor/comments/at77za/new_python_projects/">New Python projects</a> from <a href="http://www.reddit.com/r/ProgrammerHumor">r/ProgrammerHumor</a></blockquote>
<script async src="//embed.redditmedia.com/widgets/platform.js" charset="UTF-8"></script>
Up to ubuntu 19, ubuntu comes with 2 versions of python python2.x and python3.x. For ubuntu 18.04 it's python2.7 and python3.6.
! python2 --version
! python3 --version
python3 / python2 are just symlinks to the default python2.x and python 3.x versions
! ls -l $(which python3)
How to install some other version?
! python3.9 --version
If the version is not there, you still can build python binary from sources.
# git clone https://github.com/python/cpython.git
# git checkout v1.0.1
# ./configure && make && ./python --version
$ grep -rni /usr/bin/python3 /usr/ --exclude="*.py"
sbin/aptd:1:#! /usr/bin/python3
bin/keyring:1:#!/usr/bin/python3
bin/gnome-terminal:1:#!/usr/bin/python3
...
! black --version
! which black
! head -n1 $(which black)
Run the tool with different python version
! python3.6 -m black --version
Of course i had installed black for python3.6, but not for python3.7:
! python3.7 -m black --version
But first, install pip. The official way: https://pip.pypa.io/en/stable/installing/
# sudo python3.6 get-pip.py
! which pip3.6
# python3.8 get-pip.py --user
! which pip3.8
/home/gjklv8/.local/bin needs to be added to $PATH for this to work. Same goes for all scripts installed with pip install --user
get-pip script will create a pipX script depending on the X python version used to run get-pip.py
You can also use linux package manager to install pip
# sudo apt install python3-pip
But this option has some issues:
Pip is just a python module, so you can always use -m
# python3.7 -m pip install black --user
%%HTML
<blockquote class="reddit-card" data-card-created="1582892091"><a href="https://www.reddit.com/r/ProgrammerHumor/comments/91vtas/python_27/">Python 2.7</a> from <a href="http://www.reddit.com/r/ProgrammerHumor">r/ProgrammerHumor</a></blockquote>
<script async src="//embed.redditmedia.com/widgets/platform.js" charset="UTF-8"></script>
Virtualenv is a community maintained package, which was added to python standard library in python 3.3. Virtualenv is actually quite popular, because it has some extra features and works for both python2 and python3.
! python3.8 -m venv venv_dir
The apt package python3.8 by default doesn't include venv :(
# sudo apt install python3.8-venv
! python3.8 -m venv venv_dir
! tree -L 4 venv_dir/
Activate the virtual enviroment with venv_dir/bin/activate. deactivate will be available in PATH to exit venv. And that's it. (can't do that in ipython)
# source ./venv_dir/bin/activate
# pip install pyjokes
# python -c "import pyjokes; print(pyjokes.get_joke())"
# "An SQL query goes into a bar, walks up to two tables and asks, 'Can I join you?'"
# deactivate
# python -c "import pyjokes; print(pyjokes.get_joke())"
# Traceback (most recent call last):
# File "<string>", line 1, in <module>
# ImportError: No module named pyjokes
! tree -L 4 venv_dir/
%%HTML
<a href="https://imgflip.com/i/3qokvk"><img src="https://i.imgflip.com/3qokvk.jpg" title="made at imgflip.com"/></a>
Python looks for site-packages relative to its path. Activate just inserts venv directory to PATH and prepares deactivete command.
Pycharm does the same thing when you create new project with new virtualenv interpreter.
That's actually all you need to know about virtual environments, but there's also a lot of wrappers that provide additional functionality: