Python version installation authored by PIERRE Nicolas's avatar PIERRE Nicolas
# Python
If you need a more recent version of Python, that is not available by the system (currently 3.8). You can ask an administrator to install it. However there are some things to know.
If the version you need is not available AND more recent ones do not work, contact an administrator.
## Use virtual env from correct python version !
You should always use virtual environments on the server ! However to create them, use the python version you want to use and not the default `python` command.
If you want a python 3.11 virtualenv, run the following:
```bash
python3.11 -m venv your_venv_folder
```
## pip is broken what can I do ?
It is possible that installing packages does not work, here are some tips (replace X in the commands with the correct version, for instance 3.11).
- It is better to use virtual environments as a user. It will be more manageable for you if you have multiple projects with different dependencies.
- Run pip using `pythonX -m pip` instead of only `pip` to ensure that the correct python is used. If you don't, you'll end up with pip from python 3.8 that will not install packages that need a recent python version.
- Run `pythonX -m ensurepip --upgrade` and recreate your virtual environment (do this only if you get weird errors with the above tips)
## Admin info
I (Nicolas) added the [deadsnakes PPA](https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa) to have an easy install of python versions not available on current ubuntu (20.04 focal fossa).
### pip and virtual env issues
However installation is not straightforward because users can have issues. For python 3.11, I needed to run:
```bash
sudo apt install python3.11 python3.11-venv python3.11-distutils
```
It might not be enough because pip is not the correct version (pip and pip3 points to the system pip = pip 20 for python 3.8). To know if it is enough:
```bash
# create a venv
python3.11 -m venv venv
# activate it
source venv/bin/activate
# check pip version
pip --version # -> for 3.11 it should return something like "pip 24.0 from .../venv/python3.11/site-packages/pip (python3.11)"
```
If you get "pip ... from ... (python3.8)" then you wont be able to install correctly packages.
To solve this issue I found that running this fixed the issue (you can rerun the code above to check if it works). It has to be run by the user that needs the fix !:
```bash
# deactivate any running virtual env
deactivate
# removing your virtual env that has the incorrect pip version
rm -r venv
# upgrading pip of python3.11
python3.11 -m ensurepip --upgrade
```
\ No newline at end of file