Setting-up Python for Local Development using virtualenv in MacOS

Local Development setup is subjective and different for each developers – but I like mine to be flexible as much as possible. I for see that I would be developing on multiple projects on a same machine and each will have its own set of components and package version requirements. Not all projects will be using the latest versions of libraries so its better to plan for this early.

I would like to be able to switch between different projects without problems. Often, I use Docker (if possible), nvm for NodeJS apps or virtualenv for Python to manage different versions of libraries.

For this article, I will be setting up my Python local development with virtualenv.

What I am trying to do?

  1. Setup the following:
    • python3 and pip3
    • virtualenv
  2. Create (2) Python environments with different versions and install libraries independent from 2 environments

Steps:

  1. Install Python3 by Homebrew. If have not installed Homebrew yet, check it here. This should also install pip3 for managing Python libraries
    • brew install python3
  2. Install virtualenv by pip3.
    • pip3 install virtualenv
  3. Know the directory of python3. At the time of writing the latest version is 3.11.5 and its location in my machine is /usr/local/bin/python3. Homebrew creates a symbolic link to /usr/local/bin for all packages installed. In my machine I have also Python 3.10, 3.8, 3.7 installed in /usr/local/bin/python3.10, /usr/local/bin/python3.8, /usr/local/bin/python3.7.
    • python3 --version
    • which python3
  4. Create 2 new directories in the home directory to store files of the Python virtual environments. I included the python version in the directory name. I can also name these directories by project name that I am working on
    • mkdir ~/python3.11.5-env
    • mkdir ~/python3.7-env
  5. Create the virtual environments with the following commands. Notice that I added the pull path of my python3.7 – that is because python3 is already pointing to /usr/local/bin/python3 as seen in Step #3
    • cd ~
    • virtualenv -p python3 python3.11.5-env
    • virtualenv -p /usr/local/bin/python3.7 python3.7-env
  6. Activate the Python 3.11.5 virtual environment. From here on you can use python or pip to refer to python3 or pip3
    • source ~/python3.11.5-env/bin/activate
    • which python
    • python --version
  7. Deactivate the current virtual environment
    • deactivate
  8. Activate the Python 3.7 virtual environment, to use Python 3.7
    • source ~/python3.7-env/bin/activate
    • which python
    • python --version

Notice that:

  • the Python version is different for each environment, and if I install a package in one environment, it won’t affect the others.
  • the virtual environment is applied even when changing directory after activation.
  • the virtual environment only works on the same terminal it was activated. Closing the terminal deactivates the virtual environment.

That’s it. Happy coding!

  • September 26, 2023