Python packages have a setup.py
file that allows to easily install it while handling the dependencies.
To install a package, you commonly use the pip install
command. For example, to install numpy you run the following command:
(example_env) qdouasbin@jolly:~/Python_envs>pip install numpy
Collecting numpy
Downloading https://files.pythonhosted.org/packages/74/68/2b00ba3c7390354db2a1706291750b6b7e911f6f79c0bd2184ae04f3c6fd/numpy-1.15.4-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (24.5MB)
100% |████████████████████████████████| 24.5MB 35kB/s
Installing collected packages: numpy
Successfully installed numpy-1.15.4
If you now run the pip list
command it shows you that the numpy library has been added to the Python virtual environment:
(example_env) qdouasbin@jolly:~/Python_envs>pip list
numpy (1.15.4)
pip (9.0.3)
setuptools (39.0.1)
By default pip is trying to install from the Pypi:
The Python Package Index (PyPI) is a repository of software for the Python programming language. PyPI helps you find and install software developed and shared by the Python community. Package authors use PyPI to distribute their software.
You can also install packages from a local repository as long as it contains a setup.py
file.
For example, let’s download a GitHub repository and pip install it from the local repository. Here is the GitHub page of the project: sampleproject on GitHub
(example_env) qdouasbin@jolly:~/Python_envs>cd ~/
(example_env) qdouasbin@jolly:~>git clone https://github.com/pypa/sampleproject
Cloning into 'sampleproject'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 348 (delta 3), reused 3 (delta 0), pack-reused 336
Receiving objects: 100% (348/348), 82.45 KiB | 654.00 KiB/s, done.
Resolving deltas: 100% (169/169), done.
(example_env) qdouasbin@jolly:~>cd sampleproject/
(example_env) qdouasbin@jolly:~/sampleproject>ls -l
total 64
-rw-r--r-- 1 qdouasbin 3000 1081 15 nov 15:11 LICENSE.txt
-rw-r--r-- 1 qdouasbin 3000 133 15 nov 15:11 MANIFEST.in
-rw-r--r-- 1 qdouasbin 3000 1705 15 nov 15:11 README.md
drwxr-xr-x 3 qdouasbin 3000 96 15 nov 15:11 data
drwxr-xr-x 4 qdouasbin 3000 128 15 nov 15:11 sample
-rw-r--r-- 1 qdouasbin 3000 664 15 nov 15:11 setup.cfg
-rw-r--r-- 1 qdouasbin 3000 8302 15 nov 15:11 setup.py
drwxr-xr-x 4 qdouasbin 3000 128 15 nov 15:11 tests
-rw-r--r-- 1 qdouasbin 3000 1150 15 nov 15:11 tox.ini
The setup.py
file contains the all the information needed to install the package. For example, it contains the list of the depedencies to install for the project sampleproject
to work. For instance, it contains the following line:
install_requires=['peppercorn']
Which tells us that the package peppercorn
is needed and will be installed when installing sampleproject
.
We can install this project using pip install .
:
(example_env) qdouasbin@jolly:~/sampleproject>pip install .
Processing /Users/qdouasbin/sampleproject
Collecting peppercorn (from sampleproject==1.2.0)
Downloading https://files.pythonhosted.org/packages/14/84/d8d9c3f17bda2b6f49406982546d6f6bc0fa188a43d4e3ba9169a457ee04/peppercorn-0.6-py3-none-any.whl
Installing collected packages: peppercorn, sampleproject
Running setup.py install for sampleproject ... done
Successfully installed peppercorn-0.6 sampleproject-1.2.0
(example_env) qdouasbin@jolly:~/sampleproject>pip list
numpy (1.15.4)
peppercorn (0.6)
pip (9.0.3)
sampleproject (1.2.0)
setuptools (39.0.1)
(example_env) qdouasbin@jolly:~/sampleproject>
We can see that sampleproject
and peppercorn
have been installed.
The line Running setup.py install for sampleproject ... done
tells us more about the installation mechanism used here: the pip install
command actually run a python setup.py install
command.
Using the latter command gives more freedom and has a special feature to develop a library.
Let’s uninstall the sampleproject
and peppercorn
library.
(example_env) qdouasbin@jolly:~/sampleproject>pip uninstall peppercorn sampleproject
Uninstalling peppercorn-0.6:
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn-0.6.dist-info/INSTALLER
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn-0.6.dist-info/METADATA
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn-0.6.dist-info/RECORD
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn-0.6.dist-info/WHEEL
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn-0.6.dist-info/top_level.txt
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn/__init__.py
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn/__pycache__/__init__.cpython-36.pyc
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn/__pycache__/compat.cpython-36.pyc
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn/__pycache__/tests.cpython-36.pyc
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn/compat.py
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/peppercorn/tests.py
Proceed (y/n)? y
Successfully uninstalled peppercorn-0.6
Uninstalling sampleproject-1.2.0:
/Users/qdouasbin/Python_envs/example_env/bin/sample
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/sample/__init__.py
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/sample/__pycache__/__init__.cpython-36.pyc
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/sample/package_data.dat
/Users/qdouasbin/Python_envs/example_env/lib/python3.6/site-packages/sampleproject-1.2.0-py3.6.egg-info
/Users/qdouasbin/Python_envs/example_env/my_data/data_file
Proceed (y/n)? y
Successfully uninstalled sampleproject-1.2.0
We can install the sampleproject
library and its dependencies (namely peppercorn
) using either the python setup.py install
or the python setup.py develop
- The
python setup.py install
command will copy the compiled files in your python virtual environnement folder. - The
python setup.py develop
command will create a symbolic link from the library to your python virtual environnement folder. This allows you to code your library while using it from anywhere on your system.
The later method is now deprecated to have a better handling of package dependencies.
It is highly recommended to use pip install
option as this option install all the dependencies gathered in the setup.cfg or setup.py files. Thus you won’t have any issue with uninstalled dependencies.
Hence pip install .
should be used instead of python setup.py install
and pip install -e .
instead of python setup.py develop
.