Creating Virtual Environments With Python

Hello everyone! 🤓

Have you ever noticed how when you try to install some pip dependencies using a modern linux distro, it will show you this error message?

Error message when installing packages with pip in Debian 12

That happened to me while using Debian, also happened while using Archlinux, so I searched online and found a solution, or maybe you can call it a workaround. Since I always find myself googling for the solution again and again, I decided to just use my website to save this solution and also share it with you guys, just in case it’s useful for someone else.

For example if I try to install this little program over here, I will get the error after trying to install the requirements as advised on the project’s readme:

Error again

So what I do is execute the following command outside of the project’s root (just cd ../):

python3 -m venv ./projectDir

That will create a couple of files on that project related to the virtual environment, now let’s cd into it and take a look…

New files

Now we just need to activate our virtual environment by sourcing the activate file inside the project’s new bin folder…

source ./bin/activate

If we’re not doing anything weird with our bash prompt, then the name of the directory should be prepended to it, like this:

Venv message

When this shows up we know we are now inside the virtual environment, and we should be good to go ahead and use pip3 with no issues.

It werks

Of course this is not bulletproof as it could make a mess in the project’s directory so use at your own risk.

Thank you for reading! 🍎


Last modified on 2024-06-29