Setting Up a Virtual Environment for Python 3

This is a follow-up for my previous article on how to set up virtual environments for Python.

I recently started coding a bit again and attempted to set up a virtual environment per those older instructions and realized that they were outdated: Python 2.7 is now largely deprecated (replaced by Python 3.x) and prior packages I used to install pip (easy_install) have now been superseded by better options (homebrew).

So, with that in mind, I thought I would provide an update to that older article on how to set up a virtual environment.

  1. First off, you need to make sure that you have Python 3 installed on your computer. You can check for this by running the command…

    python3 --version

    If you get an error in return, something like this:
    python 3 version command and failed result

    …then it’s fair to assume that you don’t have Python 3 installed.

    To install Python 3, you will need to download a GCC package through Xcode (Apple ID is required for this). This is also a huge package (11GB zipped when I downloaded it), so allow for some time here. If you are installing Xcode for the first time, don’t forget to run the following command in Terminal once the application is installed in order to configure the command line integration:

    xcode-select --install

    If you’re opting for quicker options, according to this good guide, not all of Xcode is needed. So, if you want to opt for a smaller package, you can download Command Line Tools (must have an Apple account) or the even smaller OSX-GCC-Installer package. (Full disclaimer: I downloaded Xcode and therefore am not sure as to the efficacy of the alternative options provided here.)

    Once you have the GCC package installed via whatever means, you will need to install Homebrew (I know! All this just to install Python 3. I hope it’s worth it!) The main step here is to run the following command in Terminal:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

    If Homebrew has trouble installing, it’ll often tell you why and how you can restart the installation process using a specific command.

    Once Homebrew is installed, the process for installing Python 3 is very straightforward. Just run the following command in Terminal and you’re all set:

    brew install python

    Check the output code for any errors and run the version command once more to verify that Python 3 is installed:

    python3 --version
    python 3 version command and successful result

    If any errors are returned, review the outputs from the Xcode and Homebrew installations to make sure that those were installed successfully. Reference the previously linked guide on the installation of those two packages to make sure that everything was installed correctly.
  2. Now comes the easy part: in Terminal, navigate to the directory where you want to create your virtual environment.
  3. Once within the directory, create the virtual environment using the command,

    python3 -m venv project

    …where “project” is the desired name for your virtual environment. I named mine “example” in the screenshot below:
    running venv command for example virtual environment

  4. You can now activate and install packages for this virtual environment as you would historically.

    Use the following command to activate the environment:

    source project/bin/activate

    And pip commands within this configuration to install packages.

    Use the deactivate command to close out of this configuration screen:

    deactivate
    configuring a virtual environment created with venv command

  5. If you need to load or freeze requirements, the same commands as before work here, too:

    pip freeze > requirements.txt

    pip install -r requirements.txt

    If you’re installing requirements from an older version of Python, I would recommend installing them one by one from the configuration as opposed to loading the requirements using the install command.

And that’s essentially it! If you don’t have Xcode and Homebrew, this process can be quite tedious. But once those utilities are installed, creating virtual environments for Python 3 is an easy process.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: