The only thing better than writing a script that does meaningful work is sharing it with others.  In most cases the friend or colleague (or even complete stranger!) you are sharing the application with will likely have a python distribution installed.  However, even though they have python, they may lack the python package installations needed to properly run your code.  Broadly this subject is referred to as package management or the management of package dependencies.

For javascript there are a variety of applications designed to manage package dependences including npm and yarn.  However, the python specific package manager we are going to examine today is called pipenv.

Broadly pipenv works by creating initializing a virtualized python distribution within your project.  Upon startup, pipenv uses a file embedded in your project's working directory (Pipefile.lock and Pipfile) to install all necessary packages.   This ensures that all required packages (to include package versions) are installed in the virtual environment prior to running the application.   To check for code pre-requisites and install pipenv follow the steps outlined below in your mac or bash terminal (Windows).  

pipenv pre-requisites

  1. Check your python version to see if pip (python package installation app) is installed. python versions greater than 2.7 automatically have pip included in the base installation.
    $ python --version
  2. Install pipenv
    $ pip install --user pipenv

Now that you've successfully verified your installation navigate to your project folder and initialize your pipenv virtual environment.

pipenv initialization

  1. Navigate to your desired project folder
    $ cd someprojectfolder
  2. Initialize pipenv by installing your project packages
    $ pipenv install

Now it is time to work with your pipenv installation by installing your first package dependency.  

Installing Packages

  1. Install a list of packages for use in your application by using the pipenv install command. pipenv both installs and logs the dependency in your Pipfile and Pipfile.lock files contained in the project directory.
    $ pipenv install numpy pandas
  2. Run your scripts by using the pipenv prefix. This prefix ensures your python script is running the nesscessary dependencies prior to execution.
    $ pipenv run python somepythoncode.py
  3. As an alternate option to using the pipenv run prefix you can also create a shell environment that initializes all package dependencies. Once loaded you can run python without the prefix.
    $ pip shell
    After the shell initializes the prefix to the command line updates to show the creation of a new digital environment. In this 'shell' you can execute python commands normally. To exit the shell at the end of your session simply press control + d.
    (pipenv-tagforvirtualenvironment) bash-3.2$ python somepythoncode.py

Now you are fully equipped with the basic knowledge to install pipenv dependencies, install pipenv, and install packages.  To see how pipenv works in practice first install pipenv using the instructions outlined above followed by cloning my pipenv demo file located on my github site here.  From there navigate to the folder in mac terminal or bash and execute the code shown below.

pipenv demo

  1. After installing pipenv, cloning the pipenv_demo file, and navigating to the working directory of the demo run the python code with the pipenv run prefix.
    $ pipenv run python pipenv_demo.py
  2. After running the file you should observe the following output. Line 1 represents a numpy array initialized using the numpy package and line 2 is a dataframe initialized by pandas in the pipenv_demo.py file.
    [1] [1 2 3 4 5]
    [2] Col1 Col2 Col3
    0 1 2 3

Congratulations!  You have run some python code without needing to worry about package dependencies thanks to the functionality of pipenv.  Now you can create your code with sharing in mind.  If you would like to know more about pipenv or package management please reference the article resources shown below.  

I hope the article was helpful.  Till' next time.

Aaron

Article Resources

  1. https://pipenv.readthedocs.io/en/latest/basics/
  2. https://www.makeuseof.com/tag/install-pip-for-python/
  3. https://media.readthedocs.org/pdf/pipenv/latest/pipenv.pdf