出于需要,当前正在用的python 3.7环境要换成python 3.6。于是有了下面的操作。
系统内没有python 3.6的版本怎么办?
在virtualenv中貌似只能根据系统中的python版本来在项目内创建python副本,于是,这里就用到了conda。
conda的作用是python版本管理,因此
conda create --name python36 python=3.6
命令说明:
作用是在anaconda的 envs文件内创建一个名为python36的文件,里面放的就是新的python版本。(新版本的存放路径默认为envs内)
一些必要的组件(pip、 setuptools)等都默认安装最新版,十分省劲儿!
显示的相关信息:
Collecting package metadata: done
Solving environment: done
## Package Plan ##
environment location: F:\Anaconda\envs\python36
added / updated specs:
- python=3.6
The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2019.3.9 | py36_0 156 KB defaults
pip-19.1.1 | py36_0 1.9 MB defaults
python-3.6.8 | h9f7ef89_7 20.3 MB defaults
setuptools-41.0.1 | py36_0 663 KB defaults
wheel-0.33.2 | py36_0 57 KB defaults
wincertstore-0.2 | py36h7fe50ca_0 13 KB defaults
------------------------------------------------------------
Total: 23.1 MB
The following NEW packages will be INSTALLED:
certifi pkgs/main/win-64::certifi-2019.3.9-py36_0
pip pkgs/main/win-64::pip-19.1.1-py36_0
python pkgs/main/win-64::python-3.6.8-h9f7ef89_7
setuptools pkgs/main/win-64::setuptools-41.0.1-py36_0
sqlite pkgs/main/win-64::sqlite-3.28.0-he774522_0
vc pkgs/main/win-64::vc-14.1-h0510ff6_4
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.15.26706-h3a45250_4
wheel pkgs/main/win-64::wheel-0.33.2-py36_0
wincertstore pkgs/main/win-64::wincertstore-0.2-py36h7fe50ca_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
wincertstore-0.2 | 13 KB | ############################################################################ | 100%
wheel-0.33.2 | 57 KB | ############################################################################ | 100%
pip-19.1.1 | 1.9 MB | ############################################################################ | 100%
certifi-2019.3.9 | 156 KB | ############################################################################ | 100%
setuptools-41.0.1 | 663 KB | ############################################################################ | 100%
python-3.6.8 | 20.3 MB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > activate python36
#
# To deactivate an active environment, use:
# > deactivate
#
# * for power-users using bash, you must source
#
conda info --env
# conda environments:
#
base * F:\Anaconda
python36 F:\Anaconda\envs\python36
所需的python版本已经有了,那么接下来先转到项目路径下
cd 项目路径
cd E:\PythonProject\tensorflow-test\cpu-type
使用 virtualenv -p 选项 指定你要copy的python版本
我这里是将copy到的副本放在了,项目文件夹下的venv文件内。
E:\PythonProject\tensorflow-test\cpu-type>virtualenv -p F:\Anaconda\envs\python36\python.exe ./venv
看到如下信息,说明成功。
Running virtualenv with interpreter F:\Anaconda\envs\python36\python.exe
Using base prefix 'F:\\Anaconda\\envs\\python36'
New python executable in E:\PythonProject\tensorflow-test\cpu-type\venv\Scripts\python.exe
copying F:\Anaconda\envs\python36\python.exe => E:\PythonProject\tensorflow-test\cpu-type\venv\Scripts\python.exe
Installing setuptools, pip, wheel...done.
python环境 启动脚本的路径在:./venv/Scripts/active
E:\PythonProject\tensorflow-test\cpu-type>cd ./venv/Scripts
E:\PythonProject\tensorflow-test\cpu-type\venv\Scripts>activate
(venv) E:\PythonProject\tensorflow-test\cpu-type\venv\Scripts>
E:\PythonProject\tensorflow-test\cpu-type\venv\Scripts>activate
(venv) E:\PythonProject\tensorflow-test\cpu-type\venv\Scripts>deactivate
E:\PythonProject\tensorflow-test\cpu-type\venv\Scripts>
完毕!!!
file->settings内
设置项目所用的python解释器为virtualenv复制的副本解释器,这样每次启动pycharm打开项目时,Terminal会自动开启副本python环境。
日后工作中使用pip安装的包均被安装在当前项目的.\cpu-type\venv\Lib\site-packages
site-packages文件夹内。
参考资料:
https://blog.csdn.net/u014628771/article/details/80066624
https://blog.csdn.net/anyao_/article/details/79049937
.
https://www.cnblogs.com/technologylife/p/6635631.html
conda命令:
https://blog.csdn.net/zhayushui/article/details/80433768
2019年6月13日
关于移动项目到新的电脑后,项目内virtualenv环境失效的问题的解决。
在新电脑的pycharm里打开pycharm后,提示项目内virtualenv的python SDK无效
原因:virtualenv的启动文件activate、activate.csh、activate.fish里,python SDK的PATH设置的是原python SDK的环境,项目移动后,需要更新这几个文件里的PATH变量。
打开CMD,切换到项目路径下使用virtualenv -p D:\main\anaconda\python.exe ./venv
命令重新copy主python环境的副本
D:\programming\python\PycharmProjects\Final_course_design>virtualenv -p D:\main\anaconda\python.exe ./venv
Already using interpreter D:\main\anaconda\python.exe
Using base prefix 'D:\\main\\anaconda'
Overwriting D:\programming\python\PycharmProjects\Final_course_design\venv\lib\orig-prefix.txt with new content
New python executable in D:\programming\python\PycharmProjects\Final_course_design\venv\Scripts\python.exe
copying D:\main\anaconda\python.exe => D:\programming\python\PycharmProjects\Final_course_design\venv\Scripts\python.exe
Installing setuptools, pip, wheel...done.
Overwriting D:\programming\python\PycharmProjects\Final_course_design\venv\Scripts\activate.bat with new content
Overwriting D:\programming\python\PycharmProjects\Final_course_design\venv\Scripts\activate with new content
可以看到,命令执行后,对activate.bat、activate
两个文件进行了重写,项目中已经安装的库仍然存在。