Python Virtual Environment Setup
Post created at 2022-12-12 10:30
Setting your virtual environment with another python version.
While I'm writing this post, I'm using Ubuntu 22.04 LTS Jammy Jellyfish, wich has python 3.10.
But, I need to work on a project that runs on python 3.8. To assure that the development environment will be the same to the production environment, I have to setup this. I don't want to use docker in this case.
To start, we need to install required packages:
We will use the launchpad PPA's to get things done easely.
Next, we need to install the GPG keys the repositories.
Output
gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created
Now, wee need to import the GPG key.
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/deadsnakes.gpg --keyserver keyserver.ubuntu.com --recv-keys F23C5A6CF475977595C89F51BA6932366A755776
Output
gpg: key BA6932366A755776: public key "Launchpad PPA for deadsnakes" imported gpg: Total number processed: 1 gpg: imported: 1
If no errors, you can continue importing PPA. Use the command below to your installed version:
Ubuntu 22.04 LTS Jammy Jellyfish
echo 'deb [signed-by=/usr/share/keyrings/deadsnakes.gpg] https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy main' | sudo tee -a /etc/apt/sources.list.d/python.list
Ubuntu 20.04 LTS Focal Fossa
echo 'deb [signed-by=/usr/share/keyrings/deadsnakes.gpg] https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu focal main' | sudo tee -a /etc/apt/sources.list.d/python.list
Complete the python3.8 installation:
sudo apt update && sudo apt install python3.8 python3.8-venv python3.8-python3.8-distutils python3.8-dev python3.8-dbg
Update pip
Output
Defaulting to user installation because normal site-packages is not writeable Requirement already satisfied: pip in /usr/lib/python3/dist-packages (22.0.2) Collecting pip pip-22.3.1-py3-none-any.whl (2.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 2.5 MB/s eta 0:00:00 Installing collected packages: pip Successfully installed pip-22.3.1
Access your project folder on terminal:
Create your virtual environment
I like to use .venv
as virtual environment folder name, but you can use venv
or whatever you want.
Checking the folder/files created:
✦ ❯ tree -L 2 .venv
.venv
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── Activate.ps1
│ ├── pip
│ ├── pip3
│ ├── pip3.8
│ ├── python -> python3.8
│ ├── python3 -> python3.8
│ └── python3.8 -> /usr/bin/python3.8
├── include
├── lib
│ └── python3.8
├── lib64 -> lib
└── pyvenv.cfg
Activating the virtual environment
Visual Studio Code and another editors can automatically detect the virtual environment and activate it. In the terminal you should do this:
And now, you can use the python that you need:
Post-setup
After this, I usually install autopep8 and flake8 for linting/formatting.
Thanks for reading! Leave a comment and click the heart icon!
Links:
Created: September 18, 2024