A Python Setup Guide
Python’s popularity has also made it one of the reasons why installation has become notoriously complex.
The complexity arises from 3 main issues:
- Operating systems have Python installed
- Working on (data science) projects with different Python versions
- Working on (data science) projects with conflicting package dependencies
System Python
Confusion arises when new Python users see that Python is already installed on their computer and they start to install packages or update it. This python is for your operating system to perform its own tasks, not the user. If you meddle with it, you might end up breaking some component to your computer.
The Python that already exists on your computer is for your computer’s operating system. It’s not for you. Pretend it doesn’t exist.
Take note of where the system Python is with which python
(MacOS/*nix) or get-command python
(Windows), and make sure you are not using that one for your own work.
Multiple Pythons
If you do manage to install Python on your computer that is separate from your operating system, you still have the issue of sometimes needing to work with multiple python versions (e.g., 3.9.17, 3.12.0). Maybe you want to test the code base in a new version of python without “testing in production”. You now need a mechanism that lets you install different python versions and switch between them.
Virtual Environments
Finally, you need a mechanism to isolate package installs and their dependencies. You may have different dependencies that clash with one another and need separate package installation environments. Or maybe you want to test a different set of package versions before you formally update them in your existing project. Virtual environments help with this issue, by creating a separate “environments” that all your packages in one project installs into. This way the packages from one project do not clash with packages in another.
It may seem like a good idea to install all the packages you use into a single environment, but as projects get larger, you may not want to always be installing and updating packages, potentially breaking your existing code.
If this is the first time you’re learning about virtual environments, you can see the Virtual Environments section.
This Guide
This guide mainly goes through using pyenv
for installing and switching Python versions, and the built-in python venv
module to install python virtual environments.