Pipenv & Virtual Environments
This tutorial will guide you through the installation and use of Python packages.
It will show you how to install and use the necessary tools and make a strong recommendation on best practices. Keep in mind that Python is used for many different purposes. Accurately, how you want to manage dependencies may change depending on how you decide to publish the software. The guidance provided here is most directly applicable to the development and deployment of network services, including Web applications, but is also ideal for managing development and test environments for any project.
Annotations
This guide is written for Python 3. But if you still use Python 2.7 for some reason, these guidelines should work correctly.
Make sure you already have Python and Pip
Before you go any further, make sure that you have Python and that you can get it from your command line. You can check by simply running the following command:
$ python--version
You should get 3.6.2
some output like that. If you do not have Python, please install the latest 3.x version from Python.org, or refer to the Installing Python section of this guide.
Annotations
If you are a novice, you will get the following error:
PythonTraceback (most recent call last): <module>name ' python ' was not defined
This is because this command is to be run in shell* (also known as * terminal or console ). For an introduction to using the operating system's shell and interacting with Python, see the Getting Started tutorial for novice python.
In addition, you need to make sure that pip
it is available. You can check by running the following command:
$ pip--version
If you use python.org or Homebrew's installer to install Python, you should already have a PIP. If you are using Linux and are installing using the Package Manager of your operating system, you may need to install PIP separately.
Installing Pipenv
Pipenv
is a dependency manager for a Python project. If you are familiar with node. JS's npm or Ruby bundler, they are similar to these tools in their thinking. Although pip
you can install a Python package, it is still recommended to use pipenv because it is a more advanced tool that simplifies the common use of dependency management.
Use pip
to install Pipenv:
$ pip Install--user pipenv
Annotations
This is done by a user installation to prevent any system-wide packages from being compromised. If it is not in the shell after installation, pipenv
you will need to add the binary directory of the user base directory to the PATH
.
On Linux and MacOS, you can run python - M site --user-base
found User base directory, and then put bin
Add to the end of the directory. For example, the above command typically prints out ~/.local "( " ~
expands to the local-to-path for your home directory, and then ~/.local/bin
added to path
. You can permanently set path
by modify ~/.profile .
On Windows, you py -m site --user-site
locate the user base directory by running, and then you site-packages
replace the Scripts
. For example, the above command may be returned as C:\Users\Username\AppData\Roaming\Python36\site-packages
, and then you need to include in the PATH
C:\Users\Username\AppData\Roaming\Python36\Scripts
. You can permanently set up the user in the Control Panel PATH
. You may need to log out of the PATH
changes to take effect.
Install packages for your project
Pipenv manage dependencies for each project. To install the package, change to your project directory (or just an empty directory in this tutorial) and run:
CD myproject$ pipenv Install requests
Pipenv will install the awesome requests library in your project directory and create one for you Pipfile
. Pipfile
used to track dependencies in your project that need to be reinstalled, such as when you share a project with others. You should get a similar output (although the exact path shown will vary):
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.6also creating executable in ~/.local/share/virtualenvs/ Tmp-agwwambd/bin/pythoninstalling Setuptools, Pip, Wheel...done. VIRTUALENV location: ~/.local/share/virtualenvs/tmp-agwwambdinstalling requests ... Collecting requests Using cached requests-2.18.4-py2.py3-none-any.whlcollecting idna<2.7,>=2.5 (from requests) Using cached Idna-2.6-py2.py3-none-any.whlcollecting urllib3<1.23,>=1.21.1 (from requests) using cached Urllib3-1.22-py2.py3-none-any.whlcollecting chardet<3.1.0,>=3.0.2 (from requests) Using cached Chardet-3.0.4-py2.py3-none-any.whlcollecting certifi>=2017.4.17 (from requests) Using cached Certifi-2017.7.27.1-py2.py3-none-any.whlinstalling collected Packages:idna, URLLIB3, Chardet, Certifi, RequestssuccessfuLly installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22adding requests to Pipfile ' s [ Packages] ... P.S. You have excellent taste!??? ?
Use the installed package
Now that requests is installed, you can create a simple main.py
file to use it:
Requestsrequests. Get(' https://httpbin.org/ip ')print(' Your IP is {0} '). Format(response. JSON() [' origin '] )
You can then use pipenv run
this script to run:
$ pipenv Run Python main.py
You should get a similar output:
Your IP is 8.8.8.8
Use $ pipenv run
ensures that your installation package is available for your scripts. We can also generate a new shell to ensure that all commands can be used to $ pipenv shell
access the installed packages.
Next
Congratulations, you now know how to install and use the Python package!??? ?
Lower level: virtualenv
Virtualenv is a tool for creating an isolated python environment. VIRTUALENV Create a folder that contains all the necessary executables to use the packages required by the Python project.
It can be used independently, instead of pipenv.
Install virtualenv with PIP:
$ pip Install virtualenv
To test your installation
$ virtualenv--version
Basic use
- Create a virtual environment for a project:
CD My_project_folder$ virtualenv my_project
virtualenv my_project
A folder will be created in the current directory, containing the Python executable file and pip
a copy of the library so that the other packages can be installed. The name of the virtual environment (in this case my_project
) can be arbitrary, and omitting the name will place the file in the current directory.
In any directory where you run the command, this creates a copy of Python and puts it in a file called my_project
.
You can choose to use a Python interpreter (such as ' python2.7 '):
$ virtualenv-p/usr/bin/python2.7 My_project
Or use an environment variable of ' ~/.BASHRC ' to change the interpreter to global:
Virtualenvwrapper_python=/usr/bin/python2.7
- To get started with a virtual environment, it needs to be activated:
SOURCE My_project/bin/activate
The name of the current virtual environment is displayed on the left side of the prompt (for example (my_project)您的电脑:您的工程 用户名$) 以让您知道它是激活的。从现在起,任何您使用pip安装的包将会放在 ``my_project
, in a folder that is isolated from the globally installed Python.
Install packages as usual, such as:
$ pip Install requests
- If you have temporarily completed your work in a virtual environment, you can deactivate it:
$ deactivate
This will return to the system's default Python interpreter, which will also return to the default, including the installed libraries.
To delete a virtual environment, simply delete its folder. (To do so please do rm -rf my_project
)
Then after a while, you may have many virtual environments scattered throughout the system, and you will probably forget their names or locations.
Other Notes
Running the band --no-site-packages
option virtualenv
will not include a globally installed package. This can be used to keep the package list clean in case you need to access it later. (This virtualenv
is the default behavior after 1.7 and later)
In order to maintain the consistency of your environment, the current state of the "Freeze (freeze)" Environment package is a good idea. To do this, please run:
$ pip Freeze > Requirements.txt
This will create a requirements.txt
file that contains a simple list of all the packages and their respective versions in the current environment. You can use the PIP list to view the list of installed packages without generating a requirements file. This will make it easier for a different developer (or you, if you need to recreate such an environment) to install the same version of the same package later.
$ pip Install-r requirements.txt
This helps to ensure consistency between installation, deployment, and developers.
Finally, remember to exclude the virtual Environment folder from source version control and add it to the list of ignore. (view version control ignored)
Virtualenvwrapper
Virtualenvwrapper provides a series of commands that make working with virtual environments much more enjoyable. It puts all your virtual environments in one place.
Install (make sure the virtualenv is installed):
$ pip Install virtualenvwrapperworkon_home=~/envssource/usr/local/bin/virtualenvwrapper.sh
(Virtualenvwrapper's complete installation guidelines.)
For Windows, you can use Virtualenvwrapper-win.
Install (make sure the virtualenv is installed):
$ pip Install Virtualenvwrapper-win
In Windows, the default path for Workon_home is%userprofile%envs.
Basic use
- Create a virtual environment:
$ mkvirtualenv My_project
This creates the ~/Envs
folder in my_project
.
- Working on a virtual environment:
$ Workon My_project
Alternatively, you can create a project that creates a virtual environment and $WORKON_HOME
creates a project directory in. When you use it workon myproject
, it is cd
-ed to the project directory.
$ mkproject MyProject
virtualenvwrapper provides the tab completion function for the environment name. This is useful when you have many environments and it is difficult to remember their names.
workon
can also stop your current environment, so you can quickly switch between environments.
- The stop is the same:
$ deactivate
- Delete:
$ rmvirtualenv My_project
Other useful commands
-
lsvirtualenv
-
List all the environments.
-
cdvirtualenv
-
Navigate to the directory of the currently active virtual environment, such that you can browse it
site-packages
.
-
cdsitepackages
-
Similar to the above, but directly into the
site-packages
directory.
-
lssitepackages
-
displays
site-packages
the contents of the directory.
A complete list of VIRTUALENVWRAPPER commands.
Virtualenv-burrito
With Virtualenv-burrito, you can use a single-line command to have virtualenv + virtualenvwrapper environment.
Autoenv
When you cd
enter a contained .env
directory, the environment is automatically activated by autoenv.
Use brew
to install it on Mac OS x:
$ Brew Install Autoenv
On Linux:
$ git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv' source ~/.autoenv/activate.sh ' >> ~/.BASHRC
Python Virtual Environment Building encyclopedia (GO)