For those starting in Python, setting up Python Environments in Projects could prove to be tricky as there are a lot of tools and terms in the Python ecosystem to be familiar with, in order to understand the process. This post aims to make it easy for beginners to create basic Python environments by giving step-by-step guidance.
Setting up basic Python Environments involves the below three steps.
We will see how to go about the above in detail in the sections below.
The first step in building Python Working Environments is to install Python itself. Python can be installed either using an already available installer or by building from sources.
Python releases are available on Python's Official website - Downloads. You can select the required version ( For this post, I am using the latest version available at this time, Python 3.4.1) and proceed to the Download page which has links to installers for supported operating systems. Download the installer corresponding to your OS and run it.
On successful installation,
Note: We will refer to the Python installation directory as <python-base-dir> in the commands.
After the installation, you can verify if the installation is successful by running Python executable.
On Mac OS
### Testing Python installation
# Navigate to <python-base-dir>/bin
$ cd /Library/Frameworks/Python.framework/Versions/<major>.<minor>/bin
$ python3 -V
# Current installed version should be displayed
On Mac OS, you can create a symbolic link for the Python executable in /usr/local/bin pointing to the python3 / python3.4 executable in the bin directory so that python executable is available in common path.
### Creating Symbolic link for python executable $ ln -s /Library/Frameworks/Python.framework/Versions/3.4/bin/python3 /usr/local/bin/python
### Testing Python installation # Navigate to <python-base-dir> $ cd <python-base-dir> $ python.exe -V # Current installed version should be displayed Python 3.4.1
You may want to install Python from sources for two reasons.
The version release page (For 3.41, it is Python 3.4.1 Release Page) also contains links to the Python source distribution under the "Files" section. Click on “Gzipped source tarball” link and download and extract the tarball into a directory.
After unzipping the source tarball, we can use the “configure” script in the extracted directory to customize the installation (On Windows, you can use cygwin / any other terminal emulator software to work with the shell scripts). “configure” script lets you configure the installation as per your needs. It primarily gives you fine-grained control over the different directories into which different types of files (like user executables, html documentation) will be installed. It also lets you change the different language features that you want to install. For the list of different options, please see "./configure --help".
### Configuring Python installation’s base directory $ ./configure --help
If we do not want to change the different installation directories, rather just want to provide a root directory for the Python installation (with respect to which the different directories would be installed), we can use the “--prefix” option. If no options are used with “configure” script, Python installs all packages and files in the /usr/local/bin/python<major.minor> and /usr/local/lib/python<major.minor> directories. (This location is specified when you run “configure” script with --help option)
### Configuring Python installation’s base directory # Run `configure` script with --prefix option pointing to a # location of your choice for Python installation's base # directory $ ./configure --prefix=<python-base-dir>
Now that we have configured our installation, the next step is to install Python using “make install”. You may have to run this command using “sudo” on Mac OS / Unix / Linux if you do not have sufficient permissions.
$ make install
This completes the installation of Python in the directory <python-base-dir>. There will be four sub-directories created under <python-base-dir>, "bin", "lib", "share" and "include". “python<major>” executable will reside in the <python-base-dir>/bin directory. The “site-packages” directory for third-party dependencies can be located at <python-base-dir>/lib/python<major>.<minor>/site-packages.
As in the case of installation using installer, you can verify the installation by running the python executable.
### Testing the Python installation $ cd <python-base-dir>/bin $ ./python3 -V # Current installed version should be displayed Python 3.4.1
You can create a symbolic link “python” for “python<major>” executable in the <python-base-dir>.
### Creating Symbolic link for python executable $ ln -s <python-base-dir>/bin/python3 <python-base-dir>/bin/python
Modules and Packages are Python's way of maintaining reusable code. You can think of them as jars in Java or gems in Ruby. A module is the basic unit of code re-usability in Python.
There are three types of modules in Python.
The third-party dependencies required by your project are usually packages and will mostly be available at Python Package Index (PyPI) which is an official repository for third-party libraries in Python.
There are some libraries that help in packaging Python modules.The primary task of these libraries is to define a contract to the developer to specify the package metadata, and provide means of packaging the project like creating a source distribution.
There are three popular packaging libraries for Python, namely,
distutils is the native packager that comes with Python while setuptools and distribute are enhancements over it. The history of these libraries is an interesting read.
For all practical purposes today, setuptools is the preferred tool for packaging.
You can install most of the Python packages from their source distributions using distutils' basic packaging commands. After downloading the source distribution and unzipping it, you will find a setup.py file in the root directory. You should run "python setup.py install" in the root directory to install the package. The package will be installed in the Python installation's site-packages directory.
Python Package Index (PyPI) is an official repository for Python packages and hosts most of the packages you may require for your project. To install packages from PYPI you could use one of the two package managers, pip or easy_install. These two libraries are helpful for downloading, installing, and managing packages and their dependencies. “pip” is the more widely used between the two. Here is an excerpt from pip documentation about it's advantages over easy_install. ( For more information, please see pip documentation )
Before we install packages using “pip”, we should check if “pip” is already installed in <python-base-dir>/bin as “pip<major>” (“pip” is being installed out-of-the-box with the recent versions of Python). If so, we can create a symbolic link “pip” in the bin directory pointing to “pip<major>” and go ahead with installing packages. If “pip” is not already installed, the installation can be done with the help of “get-pip.py”. The below steps show how to install “pip”.
### Install pip # Update PATH Variable to include <python-base-dir>/bin while searching for commands $ export PATH=<python-base-dir>/bin:$PATH $ curl -O https://bootstrap.pypa.io/get-pip.py $ python get-pip.py
You can create a symbolic link “pip” pointing to “pip<major” executable, if it is not already available in <python-base-dir>/bin.
### Create symbolic link “pip” $ ln -s <python-base-dir>/bin/pip<major> <python-base-dir>/bin/pip
After “pip” is installed, we can proceed to use it to install Python packages.
### Install requests package using “pip”. # pip looks for the most recent version of “requests” on PYPI that is compatible with the current version of Python. $ pip install requests --verbose # The below command prints the version of installed “requests” package. $ pip list | grep requests requests (2.4.1) # Now, let us try to uninstall “requests” and try to install a different version of it. $ pip uninstall requests --verbose # Now install a different version of requests $ pip install requests==2.2.1 # Look up the installed version $ pip list | grep requests requests (2.2.1)
Note that the “requests” package is installed into the "site-packages" directory of <python-base-dir>.
Projects generally use a file to keep track of all the third-party dependencies. All the third-party dependencies for the project can be installed with “pip install -r <requirements-file>”. For more information, please see pip documentation about requirements
Read Python Working Environment in part 2 of this article.
Disclaimer: The statements and opinions expressed in this article are those of the author(s) and do not necessarily reflect the positions of Thoughtworks.