Skip to content

Instantly share code, notes, and snippets.

@javierlopeza
Last active April 20, 2023 06:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javierlopeza/2d75083d9a0301efad5df5f593737240 to your computer and use it in GitHub Desktop.
Save javierlopeza/2d75083d9a0301efad5df5f593737240 to your computer and use it in GitHub Desktop.
How to properly setup Python 3.x with virtualenvwrapper

How to properly setup Python 3.x with virtualenvwrapper

Installing Python 3.x

To install Python, do the following steps:

  1. Go to https://www.python.org/downloads/
  2. Download the latest version, e.g., v3.10.5.
  3. Double-click the downloaded .pkg (for Mac) file and follow the instructions to install Python.
  4. After the installation is completed, verify the version by invoking the following command in the command-line tool, e.g., Terminal (on Mac):
python3 -V
  1. You should see the output reflecting the Python version that you have installed.
Python 3.10.5

Installing pip

Pip is a package management tool for Python libraries. You use pip to easily install/uninstall Python packages. To install pip, do the following steps: Invoke the following command in the command-line tool to install pip:

python3 -m ensurepip -U

If pip is not available on your computer, it will proceed to do the installation. If pip already exists, the command will try to upgrade it to the latest version. After successful installation, you can check your pip version using the following command:

python3 -m pip -V

Installing Virtual Environment

In the command-line tool, invoke the following command to install the virtual environment software (virtualenv and virtualenvwrapper):

python3 -m pip install virtualenv virtualenvwrapper

You will see a message of success shown after the installation.

In your .zshrc file, include the following:

plugins=(
    git
    history-substring-search
    virtualenvwrapper
)

# Add new Python version to PATH because we will be installing 
# virtualenv related packages into its bin folder
export PATH="/Library/Frameworks/Python.framework/Versions/3.11/bin:$PATH"
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_PYTHON=$(which python3)
source virtualenvwrapper.sh

Restart your Terminal and you should be able to mkvirtualenv my_env which will create a new virtual env using Python 3.x. The oh-my-zsh plugin should activate (or workon) the virtual env automatically upon entering a directory with the same name of the virtual env. Note that for this plugin to work, the project directory must be a git repo, otherwise the env is ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment