Default python#
| |
In Ubuntu 21.04, the default Python installation are Python 3.9.4 and Python 2.7.18. They come with the Ubuntu installation and are located in /usr/bin/python. Because it usually requires special version of Python with different projects. It is always a good idea to install different versions of Python in the local.
Normal installation with Ubuntu.#
Normally, it is very easy to install in Ubuntu. Follow this excellent page to do it. The method relies on a PPA ppa:deadsnakes/ppa. However, this PPA only support up to Ubuntu 20.10. Thus, we have to turn to install Python from resource. Here is a page that may work to use PPA on higher Ubuntu version. I have not tested it yet.
Install python3.8 from Resource#
- Install required packages for Python. Some of the following packages might be missing and cannot be installed. Just delete the missing packages.
1$ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev build-essential libreadline-dev libffi-dev zlib1g-dev liblzma-dev lzma - Download the *.tgz from the resource.
- Navigate to the downloaded place and use the following command to unzip the file.
tar xf Python-3.8.9.tgz - Navigate to the unzipped folder by using
cd Python-3.8.9and run the configuration file./configure - Use the following commands to install Python 3.8. Please make sure use
sudo make. I have triedmakeandsudo make install. It turns out that Python3.8 can be installed but I cannot use pip after the installation. The error code is pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. I have no idea why it happens. I have tried to follow this page to install a new openssl inusr/local/bin. But it does NOT help.1$ sudo make && sudo make install - Now python is installed in
usr/local/bin. It will overwrite the default Python 3.9.4 and runs as default.1$ python3 -V
Package LZMA Missing#
After installing from resource, it can have an error showing ModuleNotFoundError: No module named '_lzma'. This can be due to that the installation is from source and incomplete. To solve this problem, follow this discussion. A complete solution is to install liblzma-dev and lzma using sudo apt-get install. Afterwards, re-install the python package. The complete code is as follows:
| |
Use Python built-in venv to manage virtual environments#
Use the default python#
If using python3, will use the default python to create a virtual environment.
| |
Use another version of python#
You can specify a specific version of python that you want to use in the virtual environment.
| |
Use Virtualenv to configure the virtual development environment.#
| |
py_38_env is the virtual environment name. Use source py_38_test/bin/activate to activate the virtual environment. Use deactivate to exit the virtual environment.
Useful links#
The following links are useful.
Remove the installation files from download#
| |