Skip to content

Marina Mele's site

Reflections on family, values, and personal growth

Menu
  • Home
  • About
Menu

Install Python 3 on Mac OS X and use virtualenv and virtualenvwrapper

Posted on July 12, 2014February 21, 2015 by Marina Mele

python3This post explains how to install Python 3 in a Mac OS X, both Mavericks and Yosemite. Check this post if you want to clean install Python 2.7. You can also install both! 🙂

It also shows how to use virtualenv and virtualenvwrapper with Python 3. Don’t miss it!

Like with Python 2.7, we need to install first Xcode and Homebrew.

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)"

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

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

Close your Terminal and open it again to make these changes effective.

Install Python 3

If you type

$ brew search python

you will see the available python-related packages to install, and python3 should be among them. Let’s install it!

$ brew install python3

You can check which version is installed by typing

$ python3 --version

And you can open it with:

$ python3

Moreover, when you install python with Homebrew, you also install:

  • the corresponding pip package manager, which is called pip3
  • the corresponding Setuptools
  • pyvenv, and alternative to virtualenv — cool!!

Create Virtual environments with pyvenv

Now that you have Python3 you also have pyvenv, a tool to create virtual environments (similar to virtualenv). However, there is one important remark about the version of pyvenv you have installed: only if you installed Python 3.4 or latter, pyvenv will also install pip when creating a new virtual environment.

Let’s create a new virtual envirnoment, named myenv, using pyvenv:

$ pyvenv myenv
This will create a folder named myenv in the current directory. To activate this environment just type:
$ source myenv/bin/activate
and you can start Python 3 by just typing:
$ python
Note that as you are inside the virtual environment, you don’t need to use the command python3 to open Python 3.

Virtualenvwrapper with Python 3

Many of you will be familiar with virtualenvwrapper, a nice tool to manage more easily your virtual environments.To install it, we will first install virtualenv:
$ pip install virtualenv
$ 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
We are ready to create a new virtual environment using Python 3 with
$ mkvirtualenv --python=python3_path myenv
where python3_path is the path of python3, which can be found with
$ which python3
in my case it was
$ mkvirtualenv --python=/usr/local/bin/python3 myenv
This creates a folder myenv inside the environments folder ~/.virtualenvs.The new environment will be active after running the previous command. To deactivate it, just type:

$ deactivate

and to activate it again

$ workon myenv

While being in your python3 virtual envirnoment, if you type

$ python

you activate python 3! Moreover, you can use pip to call pip3 and install python3 packages.

For example, you can install Django 1.7 using

$ pip install Django==1.7

Now, you’re ready to code!

Please give it a +1 and share it to your friends! 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