In order to work on programming projects on my new laptop, I need to install Python. I found the instructions How To Install Python 3 and Set Up a Programming Environment on Ubuntu 18.04 [Quickstart] and started going through them. At first everything was pretty straightforward, just updating and upgrading and then installing pip3 for python3.
sudo apt update sudo apt -y upgrade python3 -V sudo apt install -y python3-pip
Then I started getting more curious about what exactly is a virtual environment anyway? Last time I encountered virtual environments, I brushed it to the side in favor of focusing my attention on getting tensorflow-gpu installed.
In an attempt to try and understand this whole “virtual environment” thing, I read through the articles Python Virtual Environments: A Primer and The Hitchhiker’s Guide to Python. It seems a virtual environment keeps your python libraries and their versions grouped together so a program can run properly without dependency issues.
While both articles were clearly in favor of making a virtual environment, they did not agree on how to go about it. One suggests installing “pipenv” and the other suggests using “virtualenv”. Luckily, I found a useful thread where the user Flimm explained the different tools available to create virtual environments. Ultimately, they recommended that beginners start with using virtualenv and pip and then learn other tools as needed.
Before I brush off Pipenv completely, I want to explore what supporters have to say about it. This well written article Why Python devs should use Pipenv was very enlightening. Pipenv supposedly makes it easier to manage complex library inter-dependencies, as well as combines installation and virtual environment support into one tool.
But at the end of the day, the article Pipenv: promises a lot, delivers very little put this issue to rest. My decision really came down to future support and user base. The fact is, virtualenv and pip are officially supported by Python. Plus most users have been using these tools, so when I borrow shared code or look for help I’ll (hopefully) run into less problems.
The next step was to install key library dependencies along with venv to create a virtual environment. Venv is actually new for Python3, and can be used in place of virtualenv, but again understanding the differences is a lengthy process requiring a bunch of reading. FFS IS ANYTHING EASY WITH PYTHON???
For the sake of completing these GD effing instructions, I’m going to go through and set up venv. Then I’ll figure out wtf the difference is between all of this other stuff later.
BACK TO THE TERMINAL!
sudo apt install build-essential libssl-dev libffi-dev python3-dev sudo apt install -y python3-venv
Next I created my new virtual environment named “my_env” and specified it to use “python3.6” version. I activated it using the “source” command, and started the python interpreter. In the venv, apparently I can use “python” instead of “python3” and “pip” instead of “pip3”.
python3.6 -m venv my_env source my_env/bin/activate python
I then ran the common “Hello, World!” program, used “quit()” to exit the python interpreter, and then used “deactivate” to exit the virtual environment.
print("Hello, World!") quit() deactivate
And with that, I’ve made and tested my first virtual environment! Huzzah!!!
Since this post is already long enough I’ll save the venv vs virtualenv topic for next time! Until then I’ll let Madam Crookshanks to wave goodbye.