GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.python-guide.org — Pipenv & Virtual Environments — The Hitchhiker's Guide to Python Pipenv & Virtual Environments¶
This tutorial walks you through installing and using Python packages. It will show you how to install and use the necessary tools and make strong recommendations on best practices. Keep in mind that Python is used for a great many different purposes, and precisely how you want to manage your dependencies may change based on how you decide to publish your software. The guidance presented here is most directly applicable to the development and deployment of network services (including web applications), but is also very well suited to managing development and testing environments for any kind of project. Note This guide is written for Python 3, however, these instructions should 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 available from your command line. You can check this by simply running: $ python --version
You should get some output like Note If you’re newcomer and you get an error like this: >>> python Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'python' is not defined It’s because this command is intended to be run in a shell (also called a terminal or console). See the Python for Beginners getting started tutorial for an introduction to using your operating system’s shell and interacting with Python. Additionally, you’ll need to make sure you have pip available. You can check this by running: $ pip --version
If you installed Python from source, with an installer from python.org, or via Homebrew you should already have pip. If you’re on Linux and installed using your OS package manager, you may have to install pip separately. Installing Pipenv¶Pipenv is a dependency manager for Python projects. If you’re familiar with Node.js’ npm or Ruby’s bundler, it is similar in spirit to those tools. While pip can install Python packages, Pipenv is recommended as it’s a higher-level tool that simplifies dependency management for common use cases. Use $ pip install --user pipenv
Note This does a user installation to prevent breaking any system-wide
packages. If On Linux and macOS you can find the user base binary directory by running
On Windows you can find the user base binary directory by running
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 this tutorial) and run: $ cd project_folder $ pipenv install requests Pipenv will install the excellent Requests library and create a Creating a Pipfile for this project... Creating a virtualenv for this project... Using base prefix '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6' New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6 Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python Installing setuptools, pip, wheel...done. Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBd Installing requests... Collecting requests Using cached requests-2.18.4-py2.py3-none-any.whl Collecting idna<2.7,>=2.5 (from requests) Using cached idna-2.6-py2.py3-none-any.whl Collecting urllib3<1.23,>=1.21.1 (from requests) Using cached urllib3-1.22-py2.py3-none-any.whl Collecting chardet<3.1.0,>=3.0.2 (from requests) Using cached chardet-3.0.4-py2.py3-none-any.whl Collecting certifi>=2017.4.17 (from requests) Using cached certifi-2017.7.27.1-py2.py3-none-any.whl Installing collected packages: idna, urllib3, chardet, certifi, requests Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22 Adding requests to Pipfile's [packages]... P.S. You have excellent taste! ✨ 🍰 ✨ Using installed packages¶Now that Requests is installed you can create a simple import requests response = requests.get('https://httpbin.org/ip') print('Your IP is {0}'.format(response.json()['origin'])) Then you can run this script using $ pipenv run python main.py
You should get output similar to this: Your IP is 8.8.8.8 Using Next steps¶Congratulations, you now know how to install and use Python packages! ✨ 🍰 ✨ Lower level: virtualenv¶virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need. It can be used standalone, in place of Pipenv. Install virtualenv via pip: $ pip install virtualenv
Test your installation: $ virtualenv --version
Basic Usage¶
$ cd project_folder $ virtualenv venv
Note ‘venv’ is the general convention used globally. As it is readily available in ignore files (eg: .gitignore’) This creates a copy of Python in whichever directory you ran the command in,
placing it in a folder named You can also use the Python interpreter of your choice (like
$ virtualenv -p /usr/bin/python2.7 venv
or change the interpreter globally with an env variable in $ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
$ source venv/bin/activate The name of the current virtual environment will now appear on the left of
the prompt (e.g. For Windows, the same command mentioned in step 1 can be used to create a virtual environment. However, activating the environment requires a slightly different command. Assuming that you are in your project directory: C:\Users\SomeUser\project_folder> venv\Scripts\activate
Install packages using the $ pip install requests
$ deactivate
This puts you back to the system’s default Python interpreter with all its installed libraries. To delete a virtual environment, just delete its folder. (In this case,
it would be After a while, though, you might end up with a lot of virtual environments littered across your system, and it’s possible you’ll forget their names or where they were placed. Note Python has included venv module from version 3.3. For more details: venv. Other Notes¶Running In order to keep your environment consistent, it’s a good idea to “freeze” the current state of the environment packages. To do this, run: $ pip freeze > requirements.txt
This will create a $ pip install -r requirements.txt
This can help ensure consistency across installations, across deployments, and across developers. Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list (see Version Control Ignores). virtualenvwrapper¶virtualenvwrapper provides a set of commands which makes working with virtual environments much more pleasant. It also places all your virtual environments in one place. To install (make sure virtualenv is already installed): $ pip install virtualenvwrapper $ export WORKON_HOME=~/Envs $ source /usr/local/bin/virtualenvwrapper.sh (Full virtualenvwrapper install instructions.) For Windows, you can use the virtualenvwrapper-win. To install (make sure virtualenv is already installed): $ pip install virtualenvwrapper-win
In Windows, the default path for WORKON_HOME is %USERPROFILE%\Envs Basic Usage¶
$ mkvirtualenv project_folder
This creates the
$ workon project_folder
Alternatively, you can make a project, which creates the virtual environment,
and also a project directory inside $ mkproject project_folder
virtualenvwrapper provides tab-completion on environment names. It really helps when you have a lot of environments and have trouble remembering their names.
$ deactivate
$ rmvirtualenv venv
Other useful commands¶
virtualenv-burrito¶With virtualenv-burrito, you can have a working virtualenv + virtualenvwrapper environment in a single command. direnv¶When you Install it on Mac OS X using $ brew install direnv
On Linux follow the instructions at direnv.net ©2011-2026 Kenneth Reitz & Real Python. CC BY-NC-SA 3.0 |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |