Skip to main content

How to install Python

These guides go over the proper installation of Python 2.7 for development purposes, as well as setuptools, pip, and virtualenv setup.

Installing Python on Mac OS X

Before installing Python, you’ll need to install a C compiler.

You can download the full version of Xcode from the Mac App Store which include a C compiler.

While OS X comes with a large number of UNIX utilities, those familiar with Linux systems will notice one key component missing: a decent package manager. Homebrew fills this void.

To install Homebrew, open Terminal or your favorite OSX terminal emulator and run:

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

The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file.

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

Now, we can install Python 2.7:

$ brew install python

or Python 3:

$ brew install python3

This will take a minute or two.

Setuptools & Pip

Setuptools enables you to download and install any compliant Python software over a network (usually the Internet) with a single command (easy_install). It also enables you to add this network installation capability to your own Python software with very little work.

pip is a tool for easily installing and managing Python packages, that is recommended over easy_install. It is superior to easy_install in several ways, and is actively maintained.

Virtual Environments

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the "Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps your global site-packages directory clean and manageable.,For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.,Virtualenv, Virtualenv is a tool to create isolated Python environments. Virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.,Install Virtualenv via pip:,$ pip install virtualenv,Basic Usage,Create a virtual environment for a project:,virtualenv venv,will create a folder in the current directory which will contain the Python executable files, and a copy of the,pip, library which you can use to install other packages. The name of the virtual environment (in this case, it was,) can be anything; omitting the name will place the files in the current directory instead.,This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named,You can also use a Python interpreter of your choice.,$ virtualenv -p /usr/bin/python2.7 venv,This will use the Python interpreter in,/usr/bin/python2.7,To begin using the virtual environment, it needs to be activated:,$ source venv/bin/activate,The name of the current virtual environment will now appear on the left of the prompt (e.g.,(venv)Your-Computer:your_project UserName$),to let you know that it's active. From now on, any package that you install using pip will be placed in the,folder, isolated from the global Python installation.,Install packages as usual, for example:,$ pip install requests,If you are done working in the virtual environment for the moment, you can deactivate it:,$ deactivate,This puts you back to the system's default Python interpreter with all its installed libraries.,To delete a virtual environment, just delete its folder. (In this case, it would be,rm -rf venv,After a while, though, you might end up with a lot of virtual environments littered across your system, and its possible you'll forget their names or where they were placed.,Other Notes,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,1.7 and later.],In order to keep your environment consistent, it's a good idea to "freeze" the current state of the environment packages. To do this, run,$ pip freeze > requirements.txt, This will create a,requirements.txt, file, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using "pip list". Later, it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions:,$ pip install -r requirements.txt,This can help ensure consistency across installations, across deployments, and across developers.,Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list.,Virtualenvwrapper,virtualenvwrapper, provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place.

For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.

To start using this and see more information: Virtual Environments

[:en]

These guides go over the proper installation of Python 2.7 for development purposes, as well as setuptools, pip, and virtualenv setup.

Installing Python on Mac OS X

Before installing Python, you’ll need to install a C compiler.

You can download the full version of Xcode from the Mac App Store which include a C compiler.

While OS X comes with a large number of UNIX utilities, those familiar with Linux systems will notice one key component missing: a decent package manager. Homebrew fills this void.

To install Homebrew, open Terminal or your favorite OSX terminal emulator and run:

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

The script will explain what changes it will make and prompt you before the installation begins. Once you’ve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable. You can do this by adding the following line at the bottom of your ~/.profile file.

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

Now, we can install Python 2.7:

$ brew install python

or Python 3:

$ brew install python3

This will take a minute or two.

Setuptools & Pip

Setuptools enables you to download and install any compliant Python software over a network (usually the Internet) with a single command (easy_install). It also enables you to add this network installation capability to your own Python software with very little work.

pip is a tool for easily installing and managing Python packages, that is recommended over easy_install. It is superior to easy_install in several ways, and is actively maintained.

Virtual Environments

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the "Project X depends on version 1.x but, Project Y needs 4.x" dilemma, and keeps your global site-packages directory clean and manageable.,For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.,Virtualenv, Virtualenv is a tool to create isolated Python environments. Virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.,Install Virtualenv via pip:,$ pip install virtualenv,Basic Usage,Create a virtual environment for a project:,virtualenv venv,will create a folder in the current directory which will contain the Python executable files, and a copy of the,pip, library which you can use to install other packages. The name of the virtual environment (in this case, it was,) can be anything; omitting the name will place the files in the current directory instead.,This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named,You can also use a Python interpreter of your choice.,$ virtualenv -p /usr/bin/python2.7 venv,This will use the Python interpreter in,/usr/bin/python2.7,To begin using the virtual environment, it needs to be activated:,$ source venv/bin/activate,The name of the current virtual environment will now appear on the left of the prompt (e.g.,(venv)Your-Computer:your_project UserName$),to let you know that it's active. From now on, any package that you install using pip will be placed in the,folder, isolated from the global Python installation.,Install packages as usual, for example:,$ pip install requests,If you are done working in the virtual environment for the moment, you can deactivate it:,$ deactivate,This puts you back to the system's default Python interpreter with all its installed libraries.,To delete a virtual environment, just delete its folder. (In this case, it would be,rm -rf venv,After a while, though, you might end up with a lot of virtual environments littered across your system, and its possible you'll forget their names or where they were placed.,Other Notes,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,1.7 and later.],In order to keep your environment consistent, it's a good idea to "freeze" the current state of the environment packages. To do this, run,$ pip freeze > requirements.txt, This will create a,requirements.txt, file, which contains a simple list of all the packages in the current environment, and their respective versions. You can see the list of installed packages without the requirements format using "pip list". Later, it will be easier for a different developer (or you, if you need to re-create the environment) to install the same packages using the same versions:,$ pip install -r requirements.txt,This can help ensure consistency across installations, across deployments, and across developers.,Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list.,Virtualenvwrapper,virtualenvwrapper, provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place.

For example, you can work on a project which requires Django 1.10 while also maintaining a project which requires Django 1.8.

To start using this and see more information: Virtual Environments

Leave a Reply

Your email address will not be published. Required fields are marked *