和其他大多数现代编程语言一样,Python对包和 模块的下载、存储以及管理有其自己的一套方法。但是当我们同时开发多个项目工程的时候,不同的项目会将第三方的包存放在 相同的路径下 。这就意味着,如果有两个工程依赖同一个包,但是所需要的版本却不一样,比如项目A依赖v1.0.0,而项目B依赖v2.0.0。由于Python无法根据版本来区分包的安装路径,那么此时,就会发生版本冲突。
简介
Python学习交流群:1004391443,这里有资源共享,技术解答,还有小编从最基础的Python资料到项目实战的学习资料都有整理,希望能帮助你更了解python,学习python
本文会从
virtualenvwrapper(virtualenv的进阶版,其提供了一系列命令使得和虚拟环境工作变得便利。)
pipenv
anaconda
三个常用的Python虚拟环境配置来汇总,如有不适之处,还望各位大佬指正。 另:
- 博客撰写以 windows操作系统 为基础(囊中羞涩),后续有Money后,会更新其他系统的操作。
- 项目使用Python版本为3.7.3
virtualenvwrapper
安装
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pip install virtualenvwrapper
pip install virtualenvwrapper-win #Windows使用该命令
复制代码
</pre>
路径
详见下图指引(从左至右,依次操作)
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1558681283358" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;"></tt-image>
使用
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">mkvirtualenv test_env
workon
workon test_env
deactivate
rmvirtualenv test_env
</pre>
pipenv
一个好的程序员,果断必须熟练 阅读各种文档,源码 。追根溯源方能获取真知!
强烈推荐: pipenv文档
特性
- pipenv集成了pip,virtualenv两者的功能,且完善了两者的一些缺陷。
- pipenv使用Pipfile和Pipfile.lock,查看包的依赖关系十分方便。
- Pipfile 文件是 TOML 格式而不是 requirements.txt 这样的纯文本。一个项目对应一个 Pipfile,支持开发环境与正式环境区分。
安装
pip install pipenv
使用
常用命令大全:
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">pipenv --where 列出本地工程路径
pipenv --venv 列出虚拟环境路径
pipenv --py 列出虚拟环境的Python可执行文件
pipenv install 创建虚拟环境
pipenv isntall [moduel] 安装包
pipenv install [moduel] --dev 安装包到开发环境
pipenv uninstall[module] 卸载包
pipenv uninstall --all 卸载所有包
pipenv graph 查看包依赖
pipenv lock 生成lockfile
pipenv run python [pyfile] 运行py文件
pipenv --rm 删除虚拟环境
复制代码
</pre>
备注:直接在项目根目录下,使用 pipenv install xxx 即可创建项目环境,并安装xxx包
anaconda
Anaconda 是一个包含数据科学常用包的 Python 发行版本。其使用 conda 创建环境,以便分隔使用不同 Python 版本和不同程序包的项目。
特性
- pip仅适用于Python,conda适用于Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++等语言
- conda结合了pip和virtualenv的功能。
- pip在系统自带Python中,包的**更新/回退版本/卸载将影响其他程序;而conda不会影响系统自带Python。
安装
此处跳过安装anaconda,如有需要可参考 anaconda安装、介绍、使用
使用
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">conda --version
conda create --name <env_name> <package_names>
示例:
conda create --n test_conda_env
复制代码
</pre>
注意:
<env_name> 即创建的环境名。建议以英文命名,且不加空格,名称两边不加尖括号“<>”。
如果要安装指定的版本号,则只需要在包名后面以=和版本号的形式执行。如:conda create --name python2 python=2.7,即创建一个名为“python2”的环境,环境中安装版本为2.7的python。
如果要在新创建的环境中创建多个包,则直接在<package_names>后以空格隔开,添加多个包名即可。如:conda create -n python3 python=3.5 numpy pandas,即创建一个名为“python3”的环境,环境中安装版本为3.5的python,同时也安装了numpy和pandas。
<pre spellcheck="false" style="box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">conda info --env
activate test_conda_env
deactivate
conda remove -n test_conda_env --all
</pre>
ok,这篇关于如何创建Python项目环境的文章就暂时先告一段落。 初写博客,当然会有很多不足之处,如有错误遗漏的地方呢,还请各位老少爷们儿多多指教。
网友评论