分享

Install Python3 on OS X, virtualenv & virtualenvwrapper

 dzh1121 2015-09-07

This 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:

1
$ 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:

1
$ ruby -e "$(curl -fsSL https://raw./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:

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

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

Install Python 3

If you type

1
$ brew search python

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

1
$ brew install python3

You can check which version is installed by typing

1
$ python3 --version

And you can open it with:

1
$ 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:

1
$ pyvenv myenv

This will create a folder named myenv in the current directory. To activate this environment just type:

1
$ source myenv/bin/activate

and you can start Python 3 by just typing:

1
$ 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:
1
2
$ pip install virtualenv
$ pip install virtualenvwrapper
Next, create a folder that will contain all your virtual environments:
1
$ mkdir ~/.virtualenvs
Open your .bashrc file and add:
1
2
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
you can activate these changes by typing
1
$ source .bashrc
We are ready to create a new virtual environment using Python 3 with
1
$ mkvirtualenv --python=python3_path myenv
where python3_path is the path of python3, which can be found with
1
$ which python3
in my case it was
1
$ 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:
1
$ deactivate
and to activate it again
1
$ workon myenv
While being in your python3 virtual envirnoment, if you type
1
$ 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

1
$ pip install Django==1.7

Now, you’re ready to code!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多