Navigating the Python Jungle 🐍
Taming Versions, Packages, and the Mighty Pip
Without a doubt, Python is my favourite programming language. I ❤️ it for its conciseness, ease-of-understanding, and effectiveness. This excellence is partially thanks to the brilliant minds continually refining the language and crafting invaluable packages, which ultimately transform Python into an incredible tool for building production-level software.
However, as many of us know, this greatness comes with its own challenges, particularly the multiple Python versions (2, 2.7, 3, 3.9, and so on) and the maze of various pip versions. Often, while running a Python script, the version in play becomes a puzzle, and determining which pip corresponds to which Python version can be a head-scratching task 😕. Needless to say, this complexity leads to extreme confusion and chaos — a sentiment anyone familiar with Python’s ecosystem can relate to.
As I was tinkering with Django over a weekend, I decided to write a simple guide about this. Let’s go!
NOTE: The shell commands are running on a Mac OS, but should be easily ported to other systems
Step 1: Install Conda
We need a safe space to do all the experiements, and Conda is an amazing tool that allows us to do this very conveniently. Python has venv built-in that does something similar, but conda is much better for this.
~ conda -V # check conda version after install
conda 23.7.2
Step 2: Setup Environment
Let’s create an environment named test that will house our python installation and packages. It’s always a good idea to specify the exact python version we want to use while creating a conda environment. Then we activate the environment to start using the specific python version we just installed.
~ conda create -n test python=3.9
~ conda activate test
On my system the location of the environment is:
~/dev/software/miniconda3/envs/test
Now, let’s verify our python and pip versions.
~ which python
/Users/abhishek/dev/software/miniconda3/envs/django/bin/python
~ python -V
Python 3.9.17
~ which pip
/Users/abhishek/dev/software/miniconda3/envs/django/bin/pip
~ pip -V
pip 23.2.1 from /Users/abhishek/dev/software/miniconda3/envs/django/lib/python3.9/site-packages/pip (python 3.9)
See, how clear and easy!
Step 3: Installing packages 📦
The recommended way of installing packages is via conda, but not all packages will be there in conda registry, so in that case we can use pip. We’ll take the example of the django package here.
~ conda search django
~ conda install django
Let’s see where this gets installed
~ pip show django
Name: Django
Version: 4.1
Summary: A high-level Python web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD-3-Clause
Location: /Users/abhishek/dev/software/miniconda3/envs/django/lib/python3.9/site-packages
Requires: asgiref, s
As expected, the modules get installed in the conda environment only.
Lastly, don’t forget to deactivate the environment after you are done experimenting.
Conclusion
Python can be a mess sometimes, but Conda is a savior :)