To check if your system has any Python Version already Installed
$ python --version
$ python2 --version
$ python3 --version
If you don't have Python3 installed then follow along
$ sudo apt-get update
$ sudo apt-get install python3.6
-
Ubuntu 17.10, Ubuntu 18.04 (and above) come with Python 3.6 by default. You should be able to invoke it with the command
python3
. -
Ubuntu 16.10 and 17.04 do not come with Python 3.6 by default, but it is in the Universe repository. You should be able to install it with the following commands:
$ sudo apt-get update $ sudo apt-get install python3.6
You can then invoke it with the command
python3.6
. -
If you are using Ubuntu 14.04 or 16.04, Python 3.6 is not in the Universe repository, and you need to get it from a Personal Package Archive (PPA). For example, to install Python from the “deadsnakes” PPA, do the following:
$ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt-get update $ sudo apt-get install python3.6
As above, invoke with the command
python3.6
.
Begin by using pip
to install Pipenv and its dependencies,
pip install pipenv
Then change directory to the folder containing your Python project and initiate Pipenv,
cd my_project
pipenv install
This will create two new files, Pipfile
and Pipfile.lock
, in your project
directory, and a new virtual environment for your project if it doesn’t exist
already. If you add the --two
or --three
flags to that last command above,
it will initialise your project to use Python 2 or 3, respectively. Otherwise
the default version of Python will be used.
Pipfiles contain information about the dependencies of your project,
and supercede the requirements.txt
file that is typically used in Python
projects. If you’ve initiated Pipenv in a project with an existing
requirements.txt
file, you should install all the packages listed in that file
using Pipenv, before removing it from the project.
To install a Python package for your project use the install
keyword. For
example,
pipenv install beautifulsoup4
will install the current version of the Beautiful Soup package. A package
can be removed in a similar way with the uninstall
keyword,
pipenv uninstall beautifulsoup4
The package name, together with its version and a list of its own dependencies,
can be frozen by updating the Pipfile.lock
. This is done using the lock
keyword,
pipenv lock
It’s worth adding the Pipfiles to your Git repository, so that if another user were to clone the repository, all they would have to do is install Pipenv on their system and then type,
pipenv install
Then Pipenv would automagically locate the Pipfiles, create a new virtual environment and install the necessary packages.
There are usually some Python packages that are only required in your
development environment and not in your production environment, such
as unit testing packages. Pipenv will let you keep the two
environments separate using the --dev
flag. For example,
pipenv install --dev nose2
will install nose2, but will also associate it as a package that is only required in your development environment. This is useful because now, if you were to install your project in your production environment with,
pipenv install
the nose2 package won’t be installed by default. However, if another developer
were to clone your project into their own development environment, they could
use the --dev
flag,
pipenv install --dev
and install all the dependencies, including the development packages.
In order to activate the virtual environment associated with your Python project
you can simply use the shell
keyword,
pipenv shell
You can also invoke shell commands in your virtual environment, without
explicitly activating it first, by using the run
keyword. For example,
pipenv run which python
will run the which python
command in your virtual environment, and display the
path where the python
executable, that is associated with your virtual
environment, is located. This feature is a neat way of running your own Python
code in the virtual environment,
pipenv run python my_project.py
If you’re like me and shudder at having to type so much every time you want to run Python, you can always set up an alias in your shell, such as,
alias prp="pipenv run python"
I hope this post has shown you how to manage your Python projects with Pipenv. It has been around for less than a month now, so I, for one, will be interested to see how it develops over time. I certainly don’t want, or expect, it to become exactly like Bundler for Ruby, but I’ll definitely champion it for simplifying the management of dependencies in Python projects. I hope you do too!