When doing a project, you may use some of the modules, are used if the computer comes with python environment, and all the modules are installed in it, so that comes easily python environment is very bloated, and that time if you migrate to another the computer will also be required to re-install the corresponding module, so you can use the virtual environment, install the corresponding module in a different environment, but also the whole environment when migrating to migrate past.
Pycharm using a virtual environment
Pycharm can create a virtual environment directly in the settings, select a different development environments. Once created automatically generated in the root directory of a virtual environment file, as usual :( interpreter is version 3.6, version 2.7 here)
In this way, any of our operations and other modules will be installed in the current virtual environment without affecting other development environments.
However, yesterday to create a virtual environment in such a way occurs when an epic BUGG:
I'm choosing my interpreter (Python3.7) when there has been such a prompt,
It found that the created virtual environment, Scripts critical file is missing. For example, Mo was the interpreter! !
So I decided to give this operation melons, back on the right posture (above that has not solved the problem, seek help Gangster !!!)
Use the "right" virtual environment
step
Create a virtual environment
input the command:
virtualenv 环境名
(If you have multiple python versions can be ordered:
virtualenv -p python路径 环境名
to create a virtual environment)Into the environment under the
Scripts
folderEnter the command to execute the script:
activate
to activate the virtual environmentThen you can pip installation package in the virtual environment or do all kinds of things
Exit Virtual Environment
input the command:
deactivate
Management of virtual environments
Virtual environments can also be managed through a number of tools to use more convenient, it is recommended virtualenvwrapper
enter the command: pip install virtualenvwrapper-win
(window version), Downloadvirtualenvwrapper
Create a virtual environment
input the command:
mkvirtualenv 环境名
And create directly virtualenv difference is that, in front of a virtual environment that is created in the current folder, and this is the unity of the current user
envs
to create a folder and will automatically enter into this virtual environment如果不想在默认地方创建(
c:\user\envs
),可以新建个环境变量:WORKON_HOME
,然后里面设置默认路径如果要指定python版本,则输入:
mkvirtualenv --python=python路径(到exe文件) 环境名
进入虚拟环境
输入命令:
workon 环境名
退出虚拟环境
输入命令:
deactivate
删除虚拟环境
输入命令:
rmvirtualenv 环境名
列出虚拟环境
输入命令:
lsvirtualenv
进入到虚拟环境目录
输入命令:
cdvirtualenv 环境名
Pycharm配置
创建好虚拟环境,我们需要将此虚拟环境应运到已有的项目,或者在虚拟环境中创建新的项目。
在此处选择到虚拟环境下的Scripts
文件目录下的python解释器,然后我们就能安转需要的依赖了。
注意:使用虚拟环境的优点还在于,我们在导出requirements.txt的时候,只有项目依赖的所有模块,和正确的模块版本。
其他:
Django快速启动
- Django Server
- Python
requirements.txt
生成requirements.txt
cmd
切换至项目根目录(这个文件通常在最外层)执行pip freeze > requirements.txt
这时候项目根目录就会多一个
requirements.txt
文件,里面会记录我们项目需要的所以模块信息导入requirements.txt
在项目根目录下执行
pip install -i https://pypi.doubanio.com/simple/ -r requirements.txt
(使用豆瓣源安装)