Pipenv基本使用

使用pipenv管理虚拟环境和library

官方文档: https://docs.pipenv.org/
参考文章:
StackOverflow
Stop everything! Start using Pipenv!

先贴出命令


pip install --user --upgrade pipenv   # 用户安装pipenv

pipenv --three  # 会使用当前系统的Python3创建环境
pipenv --two  # 使用python2创建
pipenv --python 3.6 指定某一Python版本创建环境

pipenv run python 文件名
pipenv run pip ...  # 运行pip

pipenv shell 激活虚拟环境

pipenv --where 显示目录信息

pipenv --venv 显示虚拟环境信息

pipenv --py 显示Python解释器信息

pipenv install requests 安装相关模块并加入到Pipfile

pipenv install django==1.11 安装固定版本模块并加入到Pipfile

pipenv graph    # 显示依赖图

pipenv check    #检查安全漏洞

pipenv uninstall requests   # 卸载包并从Pipfile中移除

pipenv uninstall --all   # 卸载全部包

以前一直使用pip+virtualenv+virtualwrapper管理模块和环境, 但是virtualwrapper在windows上使用不太方便,而且包和环境分开管理确实经常不记得哪个是哪个了。 后来又出现一堆其他名字很像的虚拟环境管理,已经摸不清头脑了。

最后google了一下,总的来说发现,使用pipenv比较好, 它整合了 pip+virtualenv+Pipfile,能够自动处理好包的依赖问题和虚拟环境问题,是最推荐使用的虚拟环境管理。

安装pipenv:

$ pip install --user pipenv

升级pipenv

$ pip install --user --upgrade pipenv

使用pipenv

切换到项目文件夹,然后输入:

pipenv install requests

Pipenv是基于项目来管理依赖的,在安装requests依赖的时候,他会在项目文件夹中创建 Pipfile,用来track依赖,并且也是会分别创建一个virtualenv来运行这个项目。

写一个python文件:

import requests

response = requests.get('https://httpbin.org/ip')

print('Your IP is {0}'.format(response.json()['origin']))

使用pipenv管理的环境来运行:

pipenv run python main.py

这应该会输出类似的:

Your IP is 8.8.8.8

基本概念

  • 每个项目会自动的创建virtuanenv
  • When no parameters are passed to install, all packages [packages] specified will be installed.
  • To initialize a Python 3 virtual environment, run $ pipenv –three.
  • To initialize a Python 2 virtual environment, run $ pipenv –two.
  • Otherwise, whatever virtualenv defaults to will be the default.

其他命令

  • graph will show you a dependency graph of your installed dependencies.
  • shell will spawn a shell with the virtualenv activated.
  • run will run a given command from the virtualenv, with any arguments forwarded (e.g. $ pipenv run python or $ pipenv run pip freeze).
  • check checks for security vulnerabilities and asserts that PEP 508 requirements are being met by the current environment.

最后贴出Stackoverflow上有人问几个不同管理工具的区别的回答:

PyPI packages not in the standard library:

  • virtualenv is a very popular tool that creates isolated Python environments for Python libraries. If you’re not familiar with this tool, I highly recommend learning it, as it is a very useful tool, and I’ll be making comparisons to it for the rest of this answer.
    It works by installing a bunch of files in a directory (eg: env/), and then modifying the PATH environment variable to prefix it with a custom bin directory (eg: env/bin/). An exact copy of the python or python3 binary is placed in this directory, but Python is programmed to look for libraries relative to its path first, in the environment directory. It’s not part of Python’s standard library, but is officially blessed by the PyPA (Python Packaging Authority). Once activated, you can install packages in the virtual environment using pip.

  • pyenv is used to isolate Python versions. For example, you may want to test your code against Python 2.6, 2.7, 3.3, 3.4 and 3.5, so you’ll need a way to switch between them. Once activated, it prefixes the PATH environment variable with ~/.pyenv/shims, where there are special files matching the Python commands (python, pip). These are not copies of the Python-shipped commands; they are special scripts that decide on the fly which version of Python to run based on the PYENV_VERSION environment variable, or the .python-version file, or the ~/.pyenv/version file. pyenv also makes the process of downloading and installing multiple Python versions easier, using the command pyenv install.

  • pyenv-virtualenv is a plugin for pyenv by the same author as pyenv, to allow you to use pyenv and virtualenv at the same time conveniently. However, if you’re using Python 3.3 or later, pyenv-virtualenv will try to run python -m venv if it is available, instead of virtualenv. You can use virtualenv and pyenv together without pyenv-virtualenv, if you don’t want the convenience features.

  • virtualenvwrapper is a set of extensions to virtualenv (see docs). It gives you commands like mkvirtualenv, lssitepackages, and especially workon for switching between different virtualenv directories. This tool is especially useful if you want multiple virtualenv directories.

  • pyenv-virtualenvwrapper is a plugin for pyenv by the same author as pyenv, to conveniently integrate virtualenvwrapper into pyenv.

  • pipenv, by Kenneth Reitz (the author of requests), is the newest project in this list. It aims to combine Pipfile, pip and virtualenv into one command on the command-line. The virtualenv directory typically gets placed in ~/.local/share/virtualenvs/XXX, with XXX being a hash of the path of the project directory. This is different from virtualenv, where the directory is typically in the current working directory.

The Python Packaging Guide recommends pipenv when developing Python applications (as opposed to libraries). There does not seem to be any plans to support venv instead of virtualenv (#15). Confusingly, its command-line option –venv refers to the virtualenv directory, not venv, and similarly, the environment variable PIPENV_VENV_IN_PROJECT affects the location of the virtualenv directory, not venv directory (#1919).

Standard library:

  • pyvenv is a script shipped with Python 3 but deprecated in Python 3.6 as it had problems (not to mention the confusing name). In Python 3.6+, the exact equivalent is python3 -m venv.

  • venv is a package shipped with Python 3, which you can run using python3 -m venv (although for some reason some distros separate it out into a separate distro package, such as python3-venv on Ubuntu/Debian). It serves a similar purpose to virtualenv, and works in a very similar way, but it doesn’t need to copy Python binaries around (except on Windows). Use this if you don’t need to support Python 2. At the time of writing, the Python community seems to be happy with virtualenv and I haven’t heard much talk of venv.

Most of these tools complement each other. For instance, pipenv integrates pip, virtualenv and even pyenv if desired. The only tools that are true alternatives to each other here are venv and virtualenv.

Recommendation for beginners:

This is my personal recommendation for beginners: start by learning virtualenv and pip, tools which work with both Python 2 and 3 and in a variety of situations, and pick up the other tools once you start needing them.

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值