关于virtualenv python环境引用 pycharm相关配置的使用讨论

今天总算决定来搞一波以前从来没有弄清楚的环境问题,也觉得是时候弄明白了。

这里先说关于python的环境引用,再谈到virtualenv最后再谈论我使用的pycharm5.0关于是用python环境的引用。

由于以前我都是直接使用的本地python环境直接使用pip装的python包,所以基本上包都是全局的装到了python2.76的site_package下面。由于我所有环境默认使用的python interpreter 都是那个解释器,所以也一直没有出过什么问题。 但是随着包越来越多,依赖越来越复杂。我决定专门找个时间来解决这个棘手的问题。

首先就是想到了使用virtualenv来对环境进行隔离。这里介绍一下virtualenv的使用。

首先在全局配置中下载virtualenv。

pip install virtualenv

下载之后在自己需要的环境下面建一个virtualenv环境

virtualenv --no-site-packages laplace -p target_dir

这里要注意看一下virtualenv的参数。使用

virtualenv --help

获得全部参数 这里其实最后几个地方有用 我先贴出参数

  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -v, --verbose         Increase verbosity.
  -q, --quiet           Decrease verbosity.
  -p PYTHON_EXE, --python=PYTHON_EXE
                        The Python interpreter to use, e.g.,
                        --python=python2.5 will use the python2.5 interpreter
                        to create the new environment.  The default is the
                        interpreter that virtualenv was installed with
                        (/usr/bin/python)
  --clear               Clear out the non-root install and start from scratch.
  --no-site-packages    DEPRECATED. Retained only for backward compatibility.
                        Not having access to global site-packages is now the
                        default behavior.
  --system-site-packages
                        Give the virtual environment access to the global
                        site-packages.
  --always-copy         Always copy files rather than symlinking.
  --unzip-setuptools    Unzip Setuptools when installing it.
  --relocatable         Make an EXISTING virtualenv environment relocatable.
                        This fixes up scripts and makes all .pth files
                        relative.
  --no-setuptools       Do not install setuptools (or pip) in the new
                        virtualenv.
  --no-pip              Do not install pip in the new virtualenv.
  --no-wheel            Do not install wheel in the new virtualenv.
  --extra-search-dir=DIR
                        Directory to look for setuptools/pip distributions in.
                        This option can be used multiple times.
  --never-download      DEPRECATED. Retained only for backward compatibility.
                        This option has no effect. Virtualenv never downloads
                        pip or setuptools.
  --prompt=PROMPT       Provides an alternative prompt prefix for this
                        environment.
  --setuptools          DEPRECATED. Retained only for backward compatibility.
                        This option has no effect.
  --distribute          DEPRECATED. Retained only for backward compatibility.
                        This option has no effect.

-v -q都是关于打信息的

-p 这个参数非常重要,这里是建立环境的时候你所要使用的python 使用的路径和你要使用的版本指向

--no-site-packages 这个参数虽然已经写着废弃,但是我使用了一下还是能达到正常的效果,就是建立一个纯正的环境里面只包含必须的安装包的pip 工具和pip依赖的easy_install 还有wheel工具 其他的包一概没有。

其他的参数 大家自己去研究吧 让我们继续向下

指定好了环境里面需要使用的python版本,以及安装包所需要的工具,基本上就算是一个完整的环境了。这个时候就可以在这个目录下面打造你自己想要配置的环境了,而且互相之间丝毫不回影响到什么。

这样做的好处:

除了我最开始提到的为了避免包混乱的问题,最好的使用理由就是在不同的环境里面可以使用不同的版本,丝毫不会受到其他的限制。 而且当你在接手一个全新的应用的时候你可能会拿到requirement.txt这个由 pip freeze命令生成的文件。 那么你只需要做的事情就是用virtualenv创建一个环境,然后用pip install -r 安装这个列表里面的相关文件。就能得到一个完整的环境。

最后关于清理环境的问题, 使用环境用得很high 用完了想清理掉。 直接删掉包下面 对应的目录就可以了

rm -rf laplace/

轻松愉悦?

最后提一下我使用的ide pycharm对于virtualenv的兼容。 其实pycharm 5.0的功能一经异常强大,不仅可以兼容virtualenv,也能支持conda。

这里添加已经建立好的环境只需要使用command+, 呼出 pycharm的配置界面,然后找到interpreter 找到自己的项目 project interpreter 设置到自己virtualenv目录下面bin目录下面使用的interpreter就可以了。

创建环境 太简单了就不说了。 下一篇不出意外 我应该会再折腾一个conda的环境。 因为我现在使用的环境似乎依然 不能跑我的项目,但是在测试服务器上却可以。悲剧的是测试服务器使用的是conda。哎。。

一定是哪里没有弄对

相信科学啊

原文地址:https://www.cnblogs.com/piperck/p/5151582.html