Pipenv & Virtual Environments

https://d33wubrfki0l68.cloudfront.net/65ceaa545321e4361a74c32f66474929014b44b4/68792/_images/35294660055_42c02b2316_k_d.jpg
This tutorial walks you through installing and using Python packages.

It will show you how to install and use the necessary tools and make strongrecommendations on best practices. Keep in mind that Python is used for a greatmany different purposes, and precisely how you want to manage your dependenciesmay change based on how you decide to publish your software. The guidancepresented here is most directly applicable to the development and deployment ofnetwork services (including web applications), but is also very well suited tomanaging development and testing environments for any kind of project.

Note

This guide is written for Python 3, however, these instructionsshould work fine on Python 2.7—if you are still using it, for some reason.

Make sure you’ve got Python & pip

Before you go any further, make sure you have Python and that it’s availablefrom your command line. You can check this by simply running:

  1. $ python --version

You should get some output like 3.6.2. If you do not have Python, pleaseinstall the latest 3.x version from python.org or refer to theInstalling Python section of this guide.

Note

If you’re newcomer and you get an error like this:

  1. >>> python
  2. Traceback (most recent call last):
  3. File "<stdin>", line 1, in <module>
  4. NameError: name 'python' is not defined

It’s because this command is intended to be run in a shell (also calleda terminal or console). See the Python for Beginnersgetting started tutorial for an introduction to using your operatingsystem’s shell and interacting with Python.

Additionally, you’ll need to make sure you have pip available. You cancheck this by running:

  1. $ pip --version

If you installed Python from source, with an installer from python.org, orvia Homebrew you should already have pip. If you’re on Linux and installedusing your OS package manager, you may have to install pip separately.

Installing Pipenv

Pipenv is a dependency manager for Python projects. If you’re familiarwith Node.js’ npm or Ruby’s bundler, it is similar in spirit to thosetools. While pip can install Python packages, Pipenv is recommended asit’s a higher-level tool that simplifies dependency management for common usecases.

Use pip to install Pipenv:

  1. $ pip install --user pipenv

Note

This does a user installation to prevent breaking any system-widepackages. If pipenv isn’t available in your shell after installation,you’ll need to add the user base’s binary directory to your PATH.

On Linux and macOS you can find the user base binary directory by runningpython -m site —user-base and adding bin to the end. For example,this will typically print ~/.local (with ~ expanded to theabsolute path to your home directory) so you’ll need to add~/.local/bin to your PATH. You can set your PATH permanently bymodifying ~/.profile.

On Windows you can find the user base binary directory by runningpy -m site —user-site and replacing site-packages withScripts. For example, this could returnC:\Users\Username\AppData\Roaming\Python36\site-packages so you wouldneed to set your PATH to includeC:\Users\Username\AppData\Roaming\Python36\Scripts. You can set youruser PATH permanently in the Control Panel.aspx). You may need to logout for the PATH changes to take effect.

Installing packages for your project

Pipenv manages dependencies on a per-project basis. To install packages,change into your project’s directory (or just an empty directory for thistutorial) and run:

  1. $ cd myproject
  2. $ pipenv install requests

Pipenv will install the excellent Requests library and create a Pipfilefor you in your project’s directory. The Pipfile is used to track whichdependencies your project needs in case you need to re-install them, such aswhen you share your project with others. You should get output similar to this(although the exact paths shown will vary):

  1. Creating a Pipfile for this project...
  2. Creating a virtualenv for this project...
  3. Using base prefix '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6'
  4. New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6
  5. Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python
  6. Installing setuptools, pip, wheel...done.
  7.  
  8. Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBd
  9. Installing requests...
  10. Collecting requests
  11. Using cached requests-2.18.4-py2.py3-none-any.whl
  12. Collecting idna<2.7,>=2.5 (from requests)
  13. Using cached idna-2.6-py2.py3-none-any.whl
  14. Collecting urllib3<1.23,>=1.21.1 (from requests)
  15. Using cached urllib3-1.22-py2.py3-none-any.whl
  16. Collecting chardet<3.1.0,>=3.0.2 (from requests)
  17. Using cached chardet-3.0.4-py2.py3-none-any.whl
  18. Collecting certifi>=2017.4.17 (from requests)
  19. Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
  20. Installing collected packages: idna, urllib3, chardet, certifi, requests
  21. Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
  22.  
  23. Adding requests to Pipfile's [packages]...
  24. P.S. You have excellent taste! ✨ ? ✨

Using installed packages

Now that Requests is installed you can create a simple main.py file touse it:

  1. import requests
  2.  
  3. response = requests.get('https://httpbin.org/ip')
  4.  
  5. print('Your IP is {0}'.format(response.json()['origin']))

Then you can run this script using pipenv run:

  1. $ pipenv run python main.py

You should get output similar to this:

  1. Your IP is 8.8.8.8

Using $ pipenv run ensures that your installed packages are available toyour script. It’s also possible to spawn a new shell that ensures all commandshave access to your installed packages with $ pipenv shell.

Next steps

Congratulations, you now know how to install and use Python packages! ✨ ? ✨

原文: https://docs.python-guide.org/dev/virtualenvs/