Virtualenv For Mac

Running virtualenv with the option -no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later. This is the default behavior for virtualenv 1.7 and later. Virtualenv now copies the actual embedded Python binary on Mac OS X to fix a hang on Snow Leopard (10.6). Fail more gracefully on Windows when win32api is not installed. Fix site-packages taking precedent over Jython’s classpath and also specially handle the new pyclasspath entry in. Python Version¶ We recommend using the latest version of Python 3. Flask supports Python 3.5. To create a script like this, call virtualenv.createbootstrapscript, and write the result to your new bootstrapping script. Createbootstrapscript (extratext) ¶. Creates a bootstrap script from extratext, which is like this script but with extendparser, adjustoptions, and afterinstall hooks. This returns a string that (written to disk of course) can be used as a bootstrap script.

  1. Virtualenv Mac Os X Homebrew
  2. Virtualenvwrapper Mac
  3. Python Virtualenv Mac
  4. Install Virtualenv Mac
Last updated: August 14, 2020

While installing Python and Virtualenv on macOS Mojave & High Sierra can be done several ways, this tutorial will guide you through the process of configuring a stock Mac system into a solid Python development environment.

First steps

This guide assumes that you have already installed Homebrew. For details, please follow the steps in the macOS Configuration Guide.

Python

We are going to install the latest version of Python via asdf and its Python plugin. Why bother, you ask, when Apple includes Python along with macOS? Here are some reasons:

  • When using the bundled Python, macOS updates can remove your Python packages, forcing you to re-install them.
  • As new versions of Python are released, the Python bundled with macOS will become out-of-date. Building Python via asdf means you always have access to the most recent Python version.
  • Apple has made significant changes to its bundled Python, potentially resulting in hidden bugs.
  • Building Python via asdf includes the latest versions of Pip and Setuptools (Python package management tools)

Use the following command to install asdf via Homebrew:

Next we ensure asdf is loaded for both current and future shell sessions. If you are using Fish shell:

For

For Bash (the default shell on macOS up to and including Mojave):

For Zsh (the default shell on Catalina and Big Sur):

Install the asdf Python plugin and the latest version of Python:

Note the Python version number that was just installed. For the purpose of this guide, we will assume version 3.8.5, so replace that number below with the version number you actually just installed.

Set the default global Python version:

Confirm the Python version matches the latest version we just installed:

Pip

Let’s say you want to install a Python package, such as the Virtualenv environment isolation tool. While nearly every Python-related article for macOS tells the reader to install it via sudo pip install virtualenv, the downsides of this method include:

  1. installs with root permissions
  2. installs into the system /Library
  3. yields a less reliable environment when using Python built with asdf

As you might have guessed by now, we’re going to use the asdf Python plugin to install the Python packages that we want to be globally available. When installing via python -m pip […], packages will be installed to: ~/.asdf/installs/python/{version}/lib/python{version}/site-packages/

First, let’s ensure we are using the latest version of Pip and Setuptools:

In the next section, we’ll use Pip to install our first globally-available Python package.

Virtualenv

Python packages installed via Pip are global in the sense that they are available across all of your projects. That can be convenient at times, but it can also create problems. For example, sometimes one project needs the latest version of Django, while another project needs an older Django version to retain compatibility with a critical third-party extension. This is one of many use cases that Virtualenv was designed to solve. On my systems, only a handful of general-purpose Python packages (including Virtualenv) are globally available — every other package is confined to virtual environments.

With that explanation behind us, let’s install Virtualenv:

Create some directories to store our projects, virtual environments, and Pip configuration file, respectively:

We’ll then open Pip’s configuration file (which may be created if it doesn’t exist yet)…

… and add some lines to it:

Found

Now we have Virtualenv installed and ready to create new virtual environments, which we will store in ~/Virtualenvs. New virtual environments can be created via:

If you have both Python 3.7.x and 3.8.x installed and want to create a Python 3.7.8 virtual environment:

Restricting Pip to virtual environments

Virtualenv Mac Os X Homebrew

What happens if we think we are working in an active virtual environment, but there actually is no virtual environment active, and we install something via python -m pip install foobar? Well, in that case the foobar package gets installed into our global site-packages, defeating the purpose of our virtual environment isolation.

Thankfully, Pip has an undocumented setting (source) that tells it to bail out if there is no active virtual environment, which is exactly what we want. In fact, we’ve already set that above, via the require-virtualenv = true directive in Pip’s configuration file. For example, let’s see what happens when we try to install a package in the absence of an activated virtual environment:

Perfect! But once that option is set, how do we install or upgrade a global package? We can temporarily turn off this restriction by defining a new function in ~/.bashrc:

(As usual, after adding the above you must run source ~/.bash_profile for the change to take effect.)

If in the future we want to upgrade our global packages, the above function enables us to do so via:

You could achieve the same effect via PIP_REQUIRE_VIRTUALENV='0' python -m pip install --upgrade […], but that’s much more cumbersome to type every time.

Creating virtual environments

Let’s create a virtual environment for Pelican, a Python-based static site generator:

Change to the new environment and activate it via:

To install Pelican into the virtual environment, we’ll use Pip:

For more information about virtual environments, read the Virtualenv docs.

Dotfiles

These are obviously just the basic steps to getting a Python development environment configured. Feel free to also check out my dotfiles.

If you found this article to be useful, feel free to follow me on Twitter.

Virtualenvwrapper Mac

Dependencies¶

Python Virtualenv Mac

These distributions will be installed automatically when installing Flask.

  • Werkzeug implements WSGI, the standard Python interface betweenapplications and servers.

  • Jinja is a template language that renders the pages your applicationserves.

  • MarkupSafe comes with Jinja. It escapes untrusted input when renderingtemplates to avoid injection attacks.

  • ItsDangerous securely signs data to ensure its integrity. This is usedto protect Flask’s session cookie.

  • Click is a framework for writing command line applications. It providesthe flask command and allows adding custom management commands.

Optional dependencies¶

Virtualenv For Mac

Install Virtualenv Mac

These distributions will not be installed automatically. Flask will detect anduse them if you install them.

  • Blinker provides support for Signals.

  • SimpleJSON is a fast JSON implementation that is compatible withPython’s json module. It is preferred for JSON operations if it isinstalled.

  • python-dotenv enables support for Environment Variables From dotenv when running flaskcommands.

  • Watchdog provides a faster, more efficient reloader for the developmentserver.