Python Virtual Environment (Windows version)

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

  1. 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)

  2. Into the environment under the Scriptsfolder

    Enter the command to execute the script: activateto activate the virtual environment

  3. Then you can pip installation package in the virtual environment or do all kinds of things

  4. 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

  1. 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 envsto create a folder and will automatically enter into this virtual environment

    如果不想在默认地方创建(c:\user\envs),可以新建个环境变量:WORKON_HOME,然后里面设置默认路径

    如果要指定python版本,则输入:mkvirtualenv --python=python路径(到exe文件) 环境名

  2. 进入虚拟环境

    输入命令:workon 环境名

  3. 退出虚拟环境

    输入命令:deactivate

  4. 删除虚拟环境

    输入命令:rmvirtualenv 环境名

  5. 列出虚拟环境

    输入命令:lsvirtualenv

  6. 进入到虚拟环境目录

    输入命令:cdvirtualenv 环境名

Pycharm配置

创建好虚拟环境,我们需要将此虚拟环境应运到已有的项目,或者在虚拟环境中创建新的项目。

在此处选择到虚拟环境下的Scripts文件目录下的python解释器,然后我们就能安转需要的依赖了。

注意:使用虚拟环境的优点还在于,我们在导出requirements.txt的时候,只有项目依赖的所有模块,和正确的模块版本。

其他:

Django快速启动

  • Django Server

  • Python

requirements.txt

  1. 生成requirements.txt

    cmd切换至项目根目录(这个文件通常在最外层)执行pip freeze > requirements.txt

    这时候项目根目录就会多一个requirements.txt文件,里面会记录我们项目需要的所以模块信息

  2. 导入requirements.txt

    在项目根目录下执行pip install -i https://pypi.doubanio.com/simple/ -r requirements.txt(使用豆瓣源安装)

Guess you like

Origin www.cnblogs.com/jiumo/p/11959214.html