dwSun

一个很正经的IT工作者
中国非著名人工智能表演艺术家

导航

在Virtualenv中使用Idle

原创

在Virtualenv中使用Idle

本文适用于Linux系统,在DebianTesting上测试通过。

关于Virtualenv

先看一段Virtualenv这货的官方介绍

virtualenv is a tool to create isolated Python environments.

The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.

Also, what if you can’t install packages into the global site-packages directory? For instance, on a shared host.

In all these cases, virtualenv can help you. It creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either).

也就是说Virtualenv这货就像丫的名字,可以给用户提供一个虚拟的Python环境,这个环境里的东西独立于系统的Python设置,在这个虚拟环境里你可劲儿折腾,怎么折腾都不会有问题。

安装virtualenv也很简单:

pip install virtualenv

很多网上的教程要下载virtualenv的安装包之后手动安装,这里推荐使用pip从pypi上直接下载,可以让pip处理很多需要我们手动处理的问题。

Linux用户,推荐使用apt等包管理器安装:

sudo apt-get install virtualenv

关于VirtualenvWrapper

再看一段VirtualenvWrapper这货官方的介绍:

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

这货是辅助Virtualenv使用的,本身不能单独提供功能。
安装方式也很简单:

pip install virtualenvwrapper

推荐使用apt等包管理器安装:

sudo apt-get install virtualenvwrapper

Virtualenv中的Idle

Idle是python自带的编辑器,集编写调试于一体,在当前众多IDE百花齐放百家争鸣的盛世下,Idle或许不是最强大的IDE,但一定是最方便而且可用的,官方Python安装包已经包含了Idle,在Linux里面,只需要这样,就可以安装Idle:

sudo apt-get install idle

当然,apt仓库里面会有很多不同的Python版本,对应着也有会不同版本的Idle,请选择自己合适的版本。

新建一个vidle.py,内容如下:

#!/usr/bin/env python2
from idlelib.PyShell import main
if __name__ == '__main__':
    main()

其中,idlelib就是Idle对应的库。
第一行用于告诉shell需要用什么解释器执行当前脚本,由于我们要使用Virtualenv
,所以默认的的__/usr/bin/python__在这里无法正常访问我们的Virtualenv中的python环境,而每个Virtualenv中的python可执行文件所在的目录又不尽相同,在这里,使用env去自动寻找当前环境上下文中指定的Python可执行文件。

到这里我们已经可以在Virtualenv中使用Idle,只需要创建一个Virtualenv并在这个Virtualenv里面执行这个vidle.py就可以了。

user@linux:~$ mkvirtualenv test
Running virtualenv with interpreter /usr/bin/python2
New python executable in test/bin/python2
Also creating executable in test/bin/python
Installing setuptools, pip...done.
(test)user@linux:~$ ./path/to/vidle.py

如果不想每次都带上全路径,可以在系统里面创建一个链接,指向这个文件。

sudo ln /path/to/vidle.py /usr/bin/vidle.py

将vidle.py开头的pytho2改成python3,则该脚本可用于启动python3的idle:

#!/usr/bin/env python3
from idlelib.PyShell import main
if __name__ == '__main__':
    main()

posted on 2017-10-25 18:48  dwSun  阅读(903)  评论(0编辑  收藏  举报