Skip to content

Marina Mele's site

Reflections on family, values, and personal growth

Menu
  • Home
  • About
Menu

Install Python 2.7, virtualenv and virtualenvwrapper on OS X Mavericks/Yosemite

Posted on May 1, 2014February 21, 2015 by Marina Mele

This post explains how to install a clean version of Python in a Mac OS X Mavericks/Yosemite.

You’ll also learn to install and use the virtualenv and virtualenvwrapper tools to create virtual environments for your projects.

Install Xcode and Homebrew

First of all, install Xcode if you don’t have it already. You can find it in the Apple Store.

Next, we need to install the Command Line Tools of Xcode. Open a Terminal and type:

$ xcode-select --install

this should trigger a pop-up window that will ask you to install the Command Line Tools. If you have some trouble installing these tools, you might find useful this post on Stackoverflow.

Next, we need to install Homebrew. In the Terminal, type this command line:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Note: The previous line updates often, so you may want to check the official site for the updated command line –> Homebrew.

Now, we need to insert the Homebrew directory at the top of the PATH environment variable. In this way, some Homebrew installations, like Python, will take precedence over stock OS X binaries. Open or create the file ~/.bash_profile and write:

export PATH=/usr/local/bin:$PATH

Note: Usually, in Linux, Unix and Mac systems, the file .bash_profile is executed to configure your shell when you log in via console. When you are already logged in your machine and open a new terminal window, the file .bashrc is executed instead. However, Mac OS X is an exception, as its Terminal.app always runs a login shell by default in each new window, and calls the file .bash_profile. If you want to maintain two separate config files for login and non-login shells, you can set the common settings in .bashrc and import them in .bash_procfile with:

if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

This script looks for the .bashrc file, and calls it if exists. Close your Terminal and open it again to make these changes effective.

Install Python 2.7

First of all, let’s see the path of the Python that comes installed with Mavericks/Yosemite by typing:

$ which python

which should show /usr/bin/python . 

Now, we are ready to install Python 2.7 by typing:

$ brew install python

The good thing about installing Python with Homebrew is that you also install pip and Distribute, which extend the packaging and installation facilities provided by the distutils in the standard library.

And now, the python path should be:

$ which python
/usr/local/bin/python

Finally, we need to add the new Python scripts directory to your PATH. Open again the .bash_profile file and add

export PATH=/usr/local/share/python:$PATH

Close and open your Terminal to apply these changes.

Install virtualenv and virtualenvwrapper

A good practice when working in a Python project is to use a virtual environment. This allows you to have all the packages needed in one place. It is more easy to share and maintain.

To install virtualenv type in your terminal:

$ pip install virtualenv
Now, you can create a virtual environment with
$ virtualenv myenvironment

which will create the folder myenvironment. To activate this environment type:

$ source myenvironemtn/bin/activate

and to deactivate it, just type:

$ deactivate

Wrapped Python

Moreover, I recommend you also install virtualenvwrapper, which is a set of extensions that allows you to activate and change environments more easily. To install it:

$ pip install virtualenvwrapper

Next, create a folder that will contain all your virtual environments:

$ mkdir ~/.virtualenvs

Open your .bashrc file and add:

export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

you can activate these changes by typing

$ source .bashrc

or

$ source .bash_profile

if your .bash_profile calls the .bashrc, as explained earlier.

Now, you can create a virtual environment by typing:

$ mkvirtualenv myenvironment

which will create the folder ~/.virtualenvs/myenvironment, and it will be initially active.

You can change from one virtual environment to another by just typing:

$ workon myenvironment

Deactivate an environment with

$ deactivate

Delete an environment with

$ rmvirtualenv myenvironment

And list the existing environments with

$ lsvirtualenv

You can find more useful commands here.

Wooha! Now you’re ready to start working on your Python projects! 😉

Was it useful? Don’t miss all the thins you can learn here!

and please… give a g+1 and share!! Thanks! 🙂

Marina Melé
Marina Mele

Marina Mele has experience in artificial intelligence implementation and has led tech teams for over a decade. On her personal blog (marinamele.com), she writes about personal growth, family values, AI, and other topics she’s passionate about. Marina also publishes a weekly AI newsletter featuring the latest advancements and innovations in the field (marinamele.substack.com)

Leave a Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Personal Growth and Development
  • Artificial Intelligence
  • Mindful Parenting and Family Life
  • Productivity and Time Management
  • Mindfulness and Wellness
  • Values and Life Lessons
  • Posts en català
  • Other things to learn

Recent Posts

  • BlueSky Social – A Sneak Peek at the Future of Social Media
  • The Incredible Journey of AI Image Generation
  • AI and Fundamental Rights: How the AI Act Aims to Protect Individuals
  • Overcoming Regrets: Finding the Strength to Move Forward
  • Thinking Outside the Box: Creative Problem-Solving with Critical Thinking

RSS

  • Entries RSS
Follow @marina_mele
  • Cookie Policy
  • Privacy Policy
©2023 Marina Mele's site | Built using WordPress and Responsive Blogily theme by Superb
This website uses cookies to improve your experience. If you keep navigating through this website, we'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT