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.
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).
- 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 $ python --version
- Install pipenv
python $ pip install --user pipenv
Now that you've successfully verified your installation navigate to your project folder and initialize your pipenv virtual environment.
- Navigate to your desired project folder
python $ cd someprojectfolder
- Initialize pipenv by installing your project packages
python $ pipenv install
Now it is time to work with your pipenv installation by installing your first package dependency.
- 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.
python $ pipenv install numpy pandas
- Run your scripts by using the pipenv prefix. This prefix ensures your python script is running the nesscessary dependencies prior to execution.
python $ pipenv run python somepythoncode.py
- 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.
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.
python $ pip shell
python 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.
- 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.
python $ pipenv run python
- 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 2 3 4 5]
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.