python3 venv


venv puts the current directory’s name in its source code generated, so after renaming your directory, you will have to rename the activate file and the pip file.


In Python 3, there comes a virtual environment shipped with the standard lib called venv.

to create a new venv, you can simply call:

python3 -m venv <venv-name>
virtualenv VENV_NAME  # for python 2
    --system-site-pakcages to bring system packages

to activate the venv, you need source the generated shell script

source <venv-name>/bin/activate

Note: the generated activate script contains directory infomation based on your local computer and projects, so it’s NOT portable.

to deactivate it:


But, in most times, we don’t really care about the name of the virtual env, so let’s simply call it .venv, and you can add the following to your .bashrc for convinence

alias create-venv="python3 -m venv $DEFAULT_VENV_NAME"
alias activate="source $DEFAULT_VENV_NAME/bin/activate"

after you activated you venv, there would be a prompt before your shell prompt. You can see that python3 and pip3 is set to the venv copy of python3 by running:

which python3 # -> .venv/bin/python3

if you run pip install, the packages will be install in your local venv directory, so no sudo needed!

since the venv directory is not portable, the best practice would be save your dependencies is your requirements.txt file.

升级 venv 中的 Python 版本

首先,你需要跳出来 venv, 到一个全局环境中,然后执行:

python3 -m venv --upgrade YOUR_VENV_DIRECTORY

这样就可以升级到对应的 Python 版本啦


  1. https://stackoverflow.com/questions/2170252/can-existing-virtualenv-be-upgraded-gracefully

Python 中的


installing packages

pip install <package-name>==<version>, version is optional

To install a package from git repository:

pip install -e git+REPO

pip currently supports cloning over git, git+https and git+ssh, Here are the supported forms:

[-e] git+git://git.myproject.org/MyProject#egg=MyProject
[-e] git+https://git.myproject.org/MyProject#egg=MyProject
[-e] git+ssh://git.myproject.org/MyProject#egg=MyProject  # for private repo, you can only use this
[-e] [email protected]:MyProject#egg=MyProject

Passing branch names, a commit hash or a tag name is possible like so:
[-e] git://git.myproject.org/[email protected]#egg=MyProject
[-e] git://git.myproject.org/[email protected]#egg=MyProject
[-e] git://git.myproject.org/[email protected]#egg=MyProject

if you add the -e(editable) option, then you can save the version info in freeze, which is exactly what you need.

upgrade a package:

pip install package-name --upgrade
pip install xxx -U


pip install --on-cache-dir xxx

requirements file

save current dependencies to a requirement file:

pip freeze > requirements.txt

install from a requirement file

pip install -r requirements.txt


可以使用 pip-autoremove 包

pip install pip-autoremove
pip-autoremove requrests -y  # requests 有严重的内存泄露,而且代码非常不优雅,强烈建议使用 httpx 代替。


众所周知,Python 的依赖管理是非常拉跨的,甚至都不支持把开发依赖和运行时的依赖分开。这就导致了调试时用到的 ipdb 等等还会出现在 requirements.txt 的问题。我做了一个简单的 work-around, 先把开发依赖装好了保存到 dev-requirements.txt 中,然后再装运行时依赖,每次 freeze 的时候把开发依赖剔除掉就好了。毕竟用到的开发时依赖还是比较少的,也不怎么变,这种方法基本能解决问题。

然后神器来了:pip install pipdeptree, 他可以把依赖显示成树形,这样我们只要根部的依赖就好啦。


pip install ipython ipdb black isort pipdeptree

然后保存到 dev-requirements.txt 中:

pipdeptree -f | grep '^\w' > dev-requirements.txt

然后再安装依赖,比如 pyyaml redis, 然后保存的时候过滤掉开发依赖:

pipdeptree -f | grep '^\w' | grep -vxFf dev-requirements.txt > requirements.txt

还可以写一个 Makefile:

    pipdeptree -f | grep '^\w' | grep -vxFf dev-requirements.txt > requirements.txt

这样只要 make freeze 就好啦

让 pip 使用国内的源

  • 阿里云 https://mirrors.aliyun.com/pypi/simple/
  • 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
  • 豆瓣 http://pypi.douban.com/simple/
  • 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
  • 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
pip install flask -i https://pypi.tuna.tsinghua.edu.cn/simple/

修改 ~/.pip/pip.conf (没有就创建一个), 内容如下:




pipenv 太垃圾了,吹得很牛逼,结果基本不能用。不知道为啥不使用官方的 venv,而要使用 virtualenv。强烈不建议使用。


  1. http://crazygit.wiseturtles.com/2018/01/08/pipenv-tour/
  2. https://stackoverflow.com/questions/5900201/pip-freeze-without-dependencies-of-installed-packages