In a previous post, I explained Why Python is the best development language.
As a python programmer, you may need to use various packages to deliver your solutions. These packages might not be all compatible with each other. This is a problem if you were to install all packages in the same environment for all your projects. It would not make sense having to manage multiple Linux virtual machines.
pipenv was created to provide developers with an easy method to set up working environments for each project. pipenv is based on virtualenv.
Install pipenv
pipenv is installed using the pip command:
$ sudo pip install pipenv
Create Work Environment
The following example creates a work environment named ‘project’ using Python 2.7
$ mkdir project $ cd project $ pipenv --two
To activate run
$ pipenv shell
Your command prompt shall be prefixed with your work environment name, mine looks like this:
(project)steve@Steven-Server:~/project$
To exit a work environment.
$ exit
Remove Work Environment
To remove a project’s virtualenv and files (when not in shell):
$ pipenv --rm $ rm Pipf*
Sample 1
create > list project packages > remove > delete files
steve@Steven-Server:~$ mkdir project steve@Steven-Server:~$ cd project steve@Steven-Server:~/project$ pipenv --two steve@Steven-Server:~/project$ pipenv shell (project)steve@Steven-Server:~/project$ pip list Launching subshell in virtual environment… . /home/steve/.local/share/virtualenvs/project-9jwDEFA1/bin/activate steve@Steven-Server:~/project$ . /home/steve/.local/share/virtualenvs/project-9jwDEFA1/bin/activate (project)steve@Steven-Server:~/project$ pip list Package Version ---------- ------- pip 18.1 setuptools 40.4.3 wheel 0.32.1 (project)steve@Steven-Server:~/project$ exit steve@Steven-Server:~/project$ pipenv --rm steve@Steven-Server:~/project$ rm Pipf*
Sample 2
create > install host packages > list project packages > remove > delete files
steve@Steven-Server:~$ mkdir project steve@Steven-Server:~$ cd project steve@Steven-Server:~/project$ pipenv --two steve@Steven-Server:~/project$ pipenv install --dev steve@Steven-Server:~/project$ pipenv shell (project)steve@Steven-Server:~/project$ pip list Launching subshell in virtual environment… . /home/steve/.local/share/virtualenvs/project-9jwDEFA1/bin/activate steve@Steven-Server:~/project$ . /home/steve/.local/share/virtualenvs/project-9jwDEFA1/bin/activate (project)steve@Steven-Server:~/project$ pip list Package Version ---------- ------- asn1crypto 0.24.0 beautifulsoup4 4.6.3 certifi 2018.8.24 ... Werkzeug 0.14.1 wheel 0.30.0 (project)steve@Steven-Server:~/project$ exit steve@Steven-Server:~/project$ pipenv --rm steve@Steven-Server:~/project$ rm Pipf*
Under the hood
You will notice that two files are created Pipfile and Pipfile.lock
The Pipfile will content all the packages for the work environment
[[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [dev-packages] [packages] flask = "*" requests = "*" [requires] python_version = "2.7"
The Pipefile.lock will provide all the dependent packages for those you installed.
... "certifi": { "hashes": [ "sha256:376690d6f16d32f9d1fe8932551d80b23e9d393a8578c5633a2ed39a64861638", "sha256:456048c7e371c089d0a77a5212fb37a2c2dce1e24146e3b7e0261736aaeaa22a" ], "version": "==2018.8.24" }, "chardet": { "hashes": [ "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" ], "version": "==3.0.4" }, "click": { "hashes": [ "sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13", "sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7" ], "version": "==7.0" ...
Usage Examples
# Create a new project using Python 3.7, specifically: $ pipenv --python 3.7 # Remove project virtualenv (inferred from current directory): $ pipenv --rm # Install all dependencies for a project (including dev): $ pipenv install --dev # Create a lockfile containing pre-releases: $ pipenv lock --pre # Show a graph of your installed dependencies: $ pipenv graph # Check your installed dependencies for security vulnerabilities: $ pipenv check # Install a local setup.py into your virtual environment/Pipfile: $ pipenv install -e . # Use a lower-level pip command: $ pipenv run pip freeze
Commands
check Checks for security vulnerabilities and against PEP 508 markers provided in Pipfile. clean Uninstalls all packages not specified in Pipfile.lock. graph Displays currently-installed dependency graph information. install Installs provided packages and adds them to Pipfile, or (if no packages are given), installs all packages from Pipfile. lock Generates Pipfile.lock. open View a given module in your editor. run Spawns a command installed into the virtualenv. shell Spawns a shell within the virtualenv. sync Installs all packages specified in Pipfile.lock. uninstall Un-installs a provided package and removes it from Pipfile.Where to go from here