avatarEdward Krueger

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5331

Abstract

<p id="829b">Finally, with Pipenv, you present to others a standardized way to install project dependencies and testing and development requirements.</p><p id="4d27">Pipenv is an environment manager and a package manager. This means that Pipenv makes it possible to create an environment with Python then download and install packages into an environment with <code>pipenv install</code>.</p><p id="ec87">This command will look to the Pipfile to create an environment the Pipfile exists; if not, Pipenv will create a Pipfile for this environment.</p><p id="1386">Packages appended to this command will be added to the Pipfile.</p><div id="f5f1"><pre>pipenv <span class="hljs-keyword">install</span> pandas matplotlib keras</pre></div><h1 id="823a">Pipenv Workflow: Terminal</h1><p id="8c15">Setting up your first Pipenv: Installing Python, Pipenv, and Pandas</p><ol><li><b>Install <a href="https://www.python.org/downloads/release/python-374/"></a></b><a href="https://www.python.org/downloads/release/python-374/">Python<b> </b></a><b>3.7</b></li><li>Open a terminal and<b> install </b>Pipenv with</li></ol><div id="db1c"><pre>pip <span class="hljs-keyword">install</span> pipenv</pre></div><p id="9bf8">3. <b>Create</b> a new directory and navigate into it.</p><div id="dc35"><pre><span class="hljs-built_in">mkdir</span> pipenv_test

<span class="hljs-built_in">cd</span> pipenv_test</pre></div><p id="0fc7">4.<b> Install</b> pandas and NumPy for the environment by running:</p><div id="3f0a"><pre>pipenv <span class="hljs-keyword">install</span> pandas numpy</pre></div><p id="8943">Note that Pipenv will create a virtual environment, a Pipfile, and a Pipfile.lock for this project.</p><p id="f386">Packages can be removed similarly with the <code>uninstall</code> keyword.</p><div id="4b82"><pre><span class="hljs-attribute">pipenv uninstall pandas</span></pre></div><p id="087e">5.<b> Activate</b> the project’s virtual environment with:</p><div id="c059"><pre>pipenv <span class="hljs-keyword">shell</span></pre></div><h1 id="f163">Test an import</h1><p id="714a">After completing the above, you should now be able to run:</p><div id="4113"><pre>pipenv <span class="hljs-keyword">shell</span></pre></div><p id="6e75">Next, open the Python interpreter with:</p><div id="91bc"><pre><span class="hljs-attribute">python</span></pre></div><p id="9b66">Then in the Python interpreter:</p><div id="c8bc"><pre><span class="hljs-keyword">import</span> pandas</pre></div><figure id="1da8"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*oCGg3SkvFFCAFA1iE2ChNw.png"><figcaption>Successful importing of Pandas</figcaption></figure><p id="7c54">If successful, you are on your way to improving your development workflow!</p><p id="c82b">If any issues arose be sure that you have run <code>pipenv shell</code>before running <code>python</code>and verify the dependencies are in the environment by running.</p><div id="eaed"><pre><span class="hljs-attribute">pipenv graph</span></pre></div><figure id="906c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*kFAfud5sc7Se6X_6xgi4VA.png"><figcaption>Pipenv dependency tree</figcaption></figure><h1 id="98de">Pipenv Workflow: Jupyter</h1><p id="68f0">Setting up your first Pipenv: Installing Pipenv, Python, and Jupyter</p><ol><li><b>Install <a href="https://www.python.org/downloads/release/python-374/"></a></b><a href="https://www.python.org/downloads/release/python-374/">Python<b> </b></a><b>3.7</b></li></ol><p id="5e5c"><b>Note: </b>3.8 was incompatible with Jupyter at the time of authorship</p><p id="aa5c">2. Open a terminal and <b>Install </b>Jupyter Lab on your machine.</p><div id="2d02"><pre>pip <span class="hljs-keyword">install</span> jupyterlab</pre></div><p id="a4a1"><b>3. Install </b>Pipenv with:</p><div id="bc65"><pre>pip <span class="hljs-keyword">install</span> pipenv</pre></div><p id="6955">4. <b>Create</b> a new directory and navigate into it.</p><div id="b2d9"><pre><span class="hljs-built_in">mkdir</span> pipenv_test <span class="hljs-built_in">cd</span> pipenv_test</pre></div><p id="5bb2">5.<b> Install</b> Ipykernel and Numpy for the environment by running:</p><div id="2907"><pre>pipenv <span class="hljs-keyword">install</span> ipykernel numpy</pre></div><p id="50a3">Note that Pipenv will create a virtual environment and a Pipfile for this project.</p><p id="6a71">6.<b> Activate</b> the project’s virtual environment with:</p><div id="248c"><pre>pipenv <span class="hljs-keyword">shell</span></pre></div><p id="1286">7.<b> Connect</b> the environment to a kernel:</p><div id="b763"><pre><span class="hljs-comment">python</span> <span class="hljs-literal">-</span><span class="hljs-comment">m ipykernel install</span> <span class="hljs-literal">--</span><span class="hljs-comment">user</span> <span class="hljs-literal">--</span><span class="hljs-comment">display</span><span class="hljs-literal">-</span><span class="hljs-comment">name pipenv_test</span> <span class="hljs-literal">--</span><span class="hljs-comment">name pipenv_test</span></pre></div><p id="90fc">After completing the above, you should now be able to run:</p><div id="c7e3"><pre><span class="hljs-attribute">jupyter notebook</span></pre></div><p id="827e">Or if you prefer the Lab user interface and added functionality.</p><div id="937e"><pre><span

Options

class="hljs-attribute">jupyter lab</span></pre></div><p id="e0c4">In<b> Jupyter Notebook,</b> select ‘new’ to see a dropdown to see a list of available kernels. Note that ‘example_env’ created in step 3 above is the name of the kernel.</p><figure id="dae3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*OCDzDkZ48Lf0AFvqiGyPhQ.png"><figcaption>Selecting a kernel in Notebook</figcaption></figure><p id="bbda">In <b>Jupyter Lab,</b> you will have to click the kernel box in the upper right to access a kernel list. Or you can click the kernel tab and select ‘change kernel’ to access the same dropdown.</p><figure id="ef2d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*HtAb-uN11Hm80nH2TQWnvg.png"><figcaption>Selecting a kernel in Lab</figcaption></figure><h1 id="09f3">Test an import</h1><p id="49ba">In a new .ipynb file, attempt to run the below code in a cell.</p><div id="5018"><pre><span class="hljs-keyword">import</span> pandas</pre></div><p id="4b33">If successful, you are on your way to improving your development workflow!</p><p id="6ab4">If any issues arose, be sure you have the correct kernel selected, that you have run <code>pipenv shell</code>before running <code>jupyter lab</code>or <code>jupyter notebook</code> and before connecting the kernel. Once again, you can verify the dependencies in the environment by running.</p><div id="3de9"><pre><span class="hljs-attribute">pipenv graph</span></pre></div><figure id="cecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*kFAfud5sc7Se6X_6xgi4VA.png"><figcaption>Pipenv dependency tree</figcaption></figure><h1 id="0ac4">Environment Management:</h1><p id="1dc2">Pipenv can be used to specify Python versions and package versions.</p><div id="f78c"><pre><span class="hljs-attribute">pipenv</span> install django=<span class="hljs-number">1</span>.<span class="hljs-number">11</span>.<span class="hljs-number">10</span></pre></div><p id="8f44">Python specification:</p><div id="fe98"><pre>pipenv --<span class="hljs-keyword">python</span> <span class="hljs-symbol"><path></span> #or pipenv --<span class="hljs-keyword">python</span> <span class="hljs-keyword">python3</span></pre></div><p id="8367">A Pipenv can be built from a <b>requirements.txt</b> file:</p><div id="7195"><pre>$ pipenv <span class="hljs-keyword">install</span> -r requirements.txt</pre></div><p id="948e">And a <b>requirements.txt</b> file can be created from a Pipfile with the package pipenv-to-requirements.</p><div id="bfc8"><pre>pipenv <span class="hljs-keyword">install</span> pipenv_to_requirements</pre></div><p id="1006">Then run:</p><div id="ca34"><pre>pipenv <span class="hljs-built_in">run</span> pipenv_to_requirements</pre></div><p id="827a">This will generate requirements.txt and, if applicable, requirements-dev.txt, in the current directory.</p><h1 id="2b7d">Development dependencies</h1><p id="0068">The Pipfile organizes dependencies into two categories, dev-packages, and packages. A package can be specified as a development dependency (not to be used in production) by appending a dev flag.</p><div id="49e2"><pre>pipenv install <span class="hljs-keyword">black</span> <span class="hljs-comment">--dev</span></pre></div><figure id="9f1a"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*n5QJCVnVrElA3djor4wgrg.png"><figcaption>Pipfile structure</figcaption></figure><h1 id="73f5">Delete an Environment</h1><p id="b982">Deleting an environment is easy using the <code>pipenv —-rm </code>command. To create the environment again, use the command <code>pipenv install</code>.</p><p id="cbab">Files in your project directory will remain. If you wish to remove these, delete the entire directory.</p><h1 id="7a4f">Conclusion</h1><p id="5e44">Having all of your packages installed in a single environment becomes an issue when you want to reproduce code. Imagine exporting a requirements.txt file that lists 200 packages when the project only needs 6.</p><p id="56af">Pipenv combines package management and virtual environment control into one tool for installing, removing, tracking, and documenting your dependencies; and to create, use, and manage your virtual environments.</p><p id="6185">It’s great to add the <a href="https://github.com/pypa/pipfile">Pipfiles</a> to your Git repository so that another user can clone the repository and install <a href="http://docs.pipenv.org/">Pipenv</a> on their system and then type:</p><div id="f915"><pre>pipenv <span class="hljs-keyword">install</span></pre></div><p id="77e5"><a href="http://docs.pipenv.org/">Pipenv</a> then locates the <a href="https://github.com/pypa/pipfile">Pipfiles</a>, creates a new virtual environment, and installs the necessary packages.</p><p id="c00d">Pipenv is great because.</p><ul><li>Virtual environments can easily be reconstructed from a Pipfile</li><li>Virtual environments easily deploy to a server</li><li>Pipenv makes hygienic package and environment management easy</li><li>Properly managed environments make it easy for collaborators to interact with and run your code</li></ul><p id="0d41">For more information, check out the Pipenv docs <a href="https://pipenv-fork.readthedocs.io/en/latest/install.html#installing-packages-for-your-project">instructions for virtual environments.</a></p></article></body>

Virtual Environments for Data Science: Running Python and Jupyter with Pipenv

Setting up a virtual environment for Python.

By: Edward Krueger Data Scientist and Instructor and Douglas Franklin Teaching Assistant and Technical Writer.

In this article, we’ll talk about virtual environments and why they are useful. You’ll learn how to set up a virtual environment in Pipenv for Python. You’ll also learn how to run a Jupyter kernel out of a Pipenv environment!

Photo by Clément H on Unsplash

Why environments matter for Data Science

Environments are the space in which developers work, learn, and create. For example, if you plan to run Python code, you must have some local software setup to practice Python. That setup is called a programming environment. These environments contain the specified tools required for a developer to create and test code. For example, an environment might contain Python and some packages. Once an environment is set up correctly, a developer can work unimpeded and seamlessly share environment specs with others.

Newer developers often install everything at the system level due to a lack of understanding of, or experience with, virtual environments. Packages installed with pip are placed at the system level. The result of doing this for every project is a bloated and unmanageable singular Python environment.

Effective environment management saves time and allows developers to create an isolated software product such that collaborators or contributors can recreate your environment and run your code.

Pipenv combines package management and virtual environment control into one tool for installing, removing, tracking, and documenting your dependencies; and to create, use, and manage your virtual environments. Pipenv is essentially pip and virtualenv wrapped together into a single product.

We have all encountered this error in our development process.

ModuleNotFoundError: No module named 'pandas'

This error implies that the module, which is also called a dependency or package, can not be found by Python. You are sure you installed pandas at some point or another, but where is it?

The primary purpose of Python virtual environments is to create an isolated environment for Python projects. Proper isolation means that each project can have its dependencies, regardless of what dependencies every other project has. The above error and many others can be avoided with proper maintenance of environments and dependencies. Sanitary environment management practice reduces dependency version conflicts between your projects and keeps the base development environment from becoming bloated with packages.

Data science and deployment issues

Data Scientists are often interdisciplinary and have not been formally taught to work collaboratively with others and push projects into production. Hence good environment and module management skills are often lacking. This can cause issues with code reproducibility or difficulty advancing or sharing a project. Reproducible data science projects are those that allow others to recreate and build upon your analysis and to reuse and modify your code easily.

Sanitary environment management practices reduce dependency version conflicts between your projects and keep the base development environment from becoming bloated and unmanageable, helping users to create reproducible projects.

Courtesy of xkcd

Pipenv: a better workflow

Pipenv’s combination of package management and virtual environment control into one tool makes it a fantastic tool for data scientists and developers.

When you begin a project with Pipenv, the tool automatically creates a virtual environment, a Pipfile, and a Pipfile.lock. The Pipfile, which is similar to a requirements.txt, handles dependency management. The Pipfile is automatically updated with the new dependencies when you use the Pipenv install.

To manage complex dependencies, Pipenv keeps a tree of our project’s dependencies in a file called Pipfile.lock; for example, old versions of dependencies that depend on other old versions of dependencies. Pipfile.lock also verifies that correct versions of dependencies are used in production.

Finally, with Pipenv, you present to others a standardized way to install project dependencies and testing and development requirements.

Pipenv is an environment manager and a package manager. This means that Pipenv makes it possible to create an environment with Python then download and install packages into an environment with pipenv install.

This command will look to the Pipfile to create an environment the Pipfile exists; if not, Pipenv will create a Pipfile for this environment.

Packages appended to this command will be added to the Pipfile.

pipenv install pandas matplotlib keras

Pipenv Workflow: Terminal

Setting up your first Pipenv: Installing Python, Pipenv, and Pandas

  1. Install Python 3.7
  2. Open a terminal and install Pipenv with
pip install pipenv

3. Create a new directory and navigate into it.

mkdir pipenv_test
cd pipenv_test

4. Install pandas and NumPy for the environment by running:

pipenv install pandas numpy

Note that Pipenv will create a virtual environment, a Pipfile, and a Pipfile.lock for this project.

Packages can be removed similarly with the uninstall keyword.

pipenv uninstall pandas

5. Activate the project’s virtual environment with:

pipenv shell

Test an import

After completing the above, you should now be able to run:

pipenv shell

Next, open the Python interpreter with:

python

Then in the Python interpreter:

import pandas
Successful importing of Pandas

If successful, you are on your way to improving your development workflow!

If any issues arose be sure that you have run pipenv shellbefore running pythonand verify the dependencies are in the environment by running.

pipenv graph
Pipenv dependency tree

Pipenv Workflow: Jupyter

Setting up your first Pipenv: Installing Pipenv, Python, and Jupyter

  1. Install Python 3.7

Note: 3.8 was incompatible with Jupyter at the time of authorship

2. Open a terminal and Install Jupyter Lab on your machine.

pip install jupyterlab

3. Install Pipenv with:

pip install pipenv

4. Create a new directory and navigate into it.

mkdir pipenv_test
cd pipenv_test

5. Install Ipykernel and Numpy for the environment by running:

pipenv install ipykernel numpy

Note that Pipenv will create a virtual environment and a Pipfile for this project.

6. Activate the project’s virtual environment with:

pipenv shell

7. Connect the environment to a kernel:

python -m ipykernel install --user --display-name pipenv_test --name pipenv_test

After completing the above, you should now be able to run:

jupyter notebook

Or if you prefer the Lab user interface and added functionality.

jupyter lab

In Jupyter Notebook, select ‘new’ to see a dropdown to see a list of available kernels. Note that ‘example_env’ created in step 3 above is the name of the kernel.

Selecting a kernel in Notebook

In Jupyter Lab, you will have to click the kernel box in the upper right to access a kernel list. Or you can click the kernel tab and select ‘change kernel’ to access the same dropdown.

Selecting a kernel in Lab

Test an import

In a new .ipynb file, attempt to run the below code in a cell.

import pandas

If successful, you are on your way to improving your development workflow!

If any issues arose, be sure you have the correct kernel selected, that you have run pipenv shellbefore running jupyter labor jupyter notebook and before connecting the kernel. Once again, you can verify the dependencies in the environment by running.

pipenv graph
Pipenv dependency tree

Environment Management:

Pipenv can be used to specify Python versions and package versions.

pipenv install django=1.11.10

Python specification:

pipenv --python <path>
#or
pipenv --python python3

A Pipenv can be built from a requirements.txt file:

$ pipenv install -r requirements.txt

And a requirements.txt file can be created from a Pipfile with the package pipenv-to-requirements.

pipenv install pipenv_to_requirements

Then run:

pipenv run pipenv_to_requirements

This will generate requirements.txt and, if applicable, requirements-dev.txt, in the current directory.

Development dependencies

The Pipfile organizes dependencies into two categories, dev-packages, and packages. A package can be specified as a development dependency (not to be used in production) by appending a dev flag.

pipenv install black --dev
Pipfile structure

Delete an Environment

Deleting an environment is easy using the pipenv —-rm command. To create the environment again, use the command pipenv install.

Files in your project directory will remain. If you wish to remove these, delete the entire directory.

Conclusion

Having all of your packages installed in a single environment becomes an issue when you want to reproduce code. Imagine exporting a requirements.txt file that lists 200 packages when the project only needs 6.

Pipenv combines package management and virtual environment control into one tool for installing, removing, tracking, and documenting your dependencies; and to create, use, and manage your virtual environments.

It’s great to add the Pipfiles to your Git repository so that another user can clone the repository and install Pipenv on their system and then type:

pipenv install

Pipenv then locates the Pipfiles, creates a new virtual environment, and installs the necessary packages.

Pipenv is great because.

  • Virtual environments can easily be reconstructed from a Pipfile
  • Virtual environments easily deploy to a server
  • Pipenv makes hygienic package and environment management easy
  • Properly managed environments make it easy for collaborators to interact with and run your code

For more information, check out the Pipenv docs instructions for virtual environments.

Data Science
Development
Pipenv
Python
Jupyter
Recommended from ReadMedium