python virtual environment

There is always a colorful river in the fairy tale town

Sometimes there are some strange problems when installing the python environment. For example, some packages can’t be installed anyway. Due to the python version, some environment deployments are really troublesome. Therefore, I recommend using a virtual environment to deploy Python. For example, a project creates a separate python virtual environment that does not interfere with other projects. There are a lot of python virtual environment tools, here mainly introduce virtualenv and pyenv.

virtualenv

Virtualenv is cross-platform, linux, mac, windows can be used.

install

1
pip install virtualenv

Creating a virtual directory

1
virtualenv kvenv -p /usr/bin/python2

Description: After the creation is completed, a kvenv directory will be generated. You can add the Python version with the -p parameter. (Of course, you must have a version of python installed to create this version of the virtual directory.)

Activating a virtual environment

1
source kvenv/bin/activate

Exiting the virtual environment

1
deactivate

View python path

1
Which python # See if the python path is a newly created virtual directory

Note: Mac, linux and windows are installed in the same way.

pinch

Pyenv is strictly a version controller of Python and is very flexible to use.

Install

1
2
$ brew update
$ brew install peen

Configuring environment variables

1
2
$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
source ~/.bash_profile

Usage

  • pyenv version # current version
  • pyenv versions # all versions
  • pyenv global system # global switch
  • pyenv local 2.7.10 # Local switch
  • pyenv local 3.5.0 –unset # cancel switching

Pyenv common commands

1
2
3
4
5
6
7
$ pyenv install --list #List installable versions
$ pyenv install <version> # Install the corresponding version
$ pyenv versions # Show current python version
$ pyenv which python # show current python installation path
$ pyenv global <version> # set the default Python version
$ pyenv local <version> # Create a .python-version for the current path, and then automatically switch to this directory after entering this directory.
$ pyenv shell <version> # Enable a version in the current shell session, with a higher priority than global and local

Install other versions of python

1
Pyenv rehash # Remember to rehash after installation

virtualenv or pyenv ?

If it is a project environment, it is recommended that virtualenv, the environment is independent, there will be no big bugs.
If you are just learning to practice python, you can use pyenv to switch easily.

本文标题:python virtual environment

文章作者:nmask

发布时间:2017年08月24日 - 14:08

最后更新:2019年07月11日 - 16:07

原始链接:https://thief.one/2017/08/24/2/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: