pip3 install pipenv
# 默认安装在~/.local/virtualenv下
mkdir project
cd project
pipenv install
# 安装在自定义目录
# 设置环境变量WORKON_HOME
export WORKON_HOME=/usr/local/venv
# 或者把环境安装在工程目录
export WORKON_HOME=PIPENV_VENV_IN_PROJECT
生成两个文件,Pipfile和Pipfile.lock,代替requirement.txt文件,Pipfile可以修改pypi地址
pipenv install requests
pipenv install requests --dev #安装到开发环境
pipenv graph
pipenv lock -r >requirements.txt
pipenv lock -r --dev >requirements.txt
pipenv install -r requirements.txt
# 方法一:
pipenv run python xxx.py
# 方法二:
切换虚拟环境
pipenv shell
退出虚拟环境用exit命令
# 方法三:
直接运行虚拟环境命令
~/.local/virtualenv/{project}/bin/python xxx.py
pipenv uninstall {module}
pipenv uninstall --all #卸载所有包
cd project
pipenv --rm
# A主机的test环境迁移到B主机
# (1)在B主机安装一个基础环境
cd /usr/local/test/
pipenv install
cd ~/.local/virtualenv/
rm test-{keyB} -rf
# (2)把A主机的环境复制过来,修改环境目录自带的key值
scp -r A:~/.local/virtualenv/test-{keyA} ./
mv ./test-{keyA} ~/.local/virtualenv/test-{keyB}
注:用pipenv虚拟anaconda的环境时特别慢,且迁移后会报错SSLError。
因为anaconda使用自带的ssl模块,而不使用linux的openssl库。
把anaconda下的libcryto.so和libssl.so两个动态库复制到虚拟环境的lib目录下即可。
pipenv --where #列出本地工程路径
pipenv --venv #列出虚拟环境路径
pipenv --py #列出虚拟环境的python可执行文件
pip3 install virtualenv
mkdir -p /data/venv/
cd /data/venv
virtualenv --no-site-packages testvenv
# 加参数--no-site-packages,已经安装到系统Python环境中的所有第三方包都不会复制过来,
# 可以得到了一个不带任何第三方包的“干净”的Python运行环境。
virtualenv --no-site-packages -p /usr/bin/python2.7 testvenv
# -p指定默认python版本
source /data/venv/testvenv/bin/activate
deactivate
cd /data/venv/
# 打包整个环境
zip -r testvenv.zip ./testvenv
# 复制到其他机器或其他位置,解压即可使用
cd /data/venv/
rm -rf testvenv
python3.3之后原生包含venv模块,python3.4开始使用venv模块生成虚拟环境自带pip
$ python -m venv -h
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
[--upgrade] [--without-pip]
ENV_DIR [ENV_DIR ...]
Creates virtual Python environments in one or more target directories.
positional arguments:
ENV_DIR A directory to create the environment in.
optional arguments:
-h, --help show this help message and exit
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
--symlinks Try to use symlinks rather than copies, when symlinks
are not the default for the platform.
--copies Try to use copies rather than symlinks, even when
symlinks are the default for the platform.
--clear Delete the contents of the environment directory if it
already exists, before environment creation.
--upgrade Upgrade the environment directory to use this version
of Python, assuming Python has been upgraded in-place.
--without-pip Skips installing or upgrading pip in the virtual
environment (pip is bootstrapped by default)
Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.
[root@optest01 ~]# python3 -m venv testvenv #生成虚拟环境
[root@optest01 ~]# source testvenv/bin/activate #进入虚拟环境
(testvenv) [root@optest01 ~]# python -V
Python 3.4.5
[root@optest01 ~]# which python
/root/testvenv/bin/python
[root@optest01 ~]# which pip
/root/testvenv/bin/pip
[root@optest01 ~]# deactivate #退出虚拟环境
https://zhuanlan.zhihu.com/p/27294128
1、Anaconda安装
(1)下载:https://www.anaconda.com/distribution/#download-section
(2)执行:bash Anaconda-xxx.sh
(3)环境变量生效:source ~/.bashrc
(4)安装成功:conda --version
2、环境、版本管理
(1)创建python2.7版本和3.6版本
conda create --name python2 python=2.7.12
conda create --name python3 python=3.6.6
默认情况下,新创建的环境将会被保存在/Users/<user_name>/anaconda3/env目录下。
其中,<user_name>为当前用户的用户名
若要创建python版本的同时添加依赖包:conda create -n python3 python=3.6 numpy=1.1 pandas
(2)切换环境
source activate python3
(3)退出环境
source deactivate
(4)显示已创建环境
conda info --envs
conda env list
(5)复制环境
conda create --name BScore --clone python3
conda create -n test --clone /usr/local/venv/py3_k2
conda create --name test --clone base # 如果在生产无法联网的环境,新建环境可以用复制基础环境的方式
(6)删除环境
conda remove --name python3 --all
(7)导出及加载环境
conda env export > env_name.yml #导出
conda create -f env_name.yml #加载
3、管理依赖包
(1)查找
conda search --full-name flask
conda search flask
(2)当前环境已安装包信息
conda list
(3)安装包
conda install --name flask
建议还是用pip,切换到指定环境使用pip进行安装 pip install flask
(4)卸载包
conda remove --name python3 flask
(5)更新指定包
conda update flask
(6)管理conda镜像
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
# 清华镜像站说明 https://mirrors.tuna.tsinghua.edu.cn/help/anaconda/
原文:https://www.cnblogs.com/maxgongzuo/p/12870295.html