如何在 Linux 上搭建 Python 开发环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2504800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set up a Python development environment on Linux?
提问by Rob Sobers
I'm a .NET developer who knows very little about Python, but want to give it a test drive for a small project I'm working on.
我是一名 .NET 开发人员,对 Python 知之甚少,但想为我正在从事的一个小项目试驾一下。
What tools and packages should I install on my machine? I'm looking for a common, somewhat comprehensive, development environment.
我应该在我的机器上安装哪些工具和包?我正在寻找一个通用的、有点全面的开发环境。
I'll likely run Ubuntu 9.10, but I'm flexible. If Windows is a better option, that's fine too.
我可能会运行 Ubuntu 9.10,但我很灵活。如果 Windows 是更好的选择,那也没关系。
Edit:To clarify, I'm not looking for the bare minimum to get a Python program to run. I wouldn't expect a newbie .NET dev to use notepad and a compiler. I'd recommend Visual Studio, NUnit, SQL Server, etc.
编辑:澄清一下,我不是在寻找让 Python 程序运行的最低要求。我不希望新手 .NET 开发人员使用记事本和编译器。我推荐 Visual Studio、NUnit、SQL Server 等。
回答by Mike Graham
Your system already has Python on it. Use the text editor or IDE of your choice; I like vim.
你的系统上已经有 Python 了。使用您选择的文本编辑器或 IDE;我喜欢vim。
I can't tell you what third-party modules you need without knowing what kind of development you will be doing. Use apt as much as you can to get the libraries.
在不知道您将进行什么样的开发的情况下,我无法告诉您需要哪些第三方模块。尽可能多地使用 apt 来获取库。
To speak to your edit:
与您的编辑交谈:
This isn't minimalistic, like handing a .NET newbie notepad and a compiler: a decent text editor and the stdlib are all you really need to start out. You will likely need third-party libraries to develop whatever kind of applications you are writing, but I cannot think of any third-party modules allPython programmers will really need or want.
这不是简单的,就像处理 .NET 新手记事本和编译器一样:一个体面的文本编辑器和 stdlib 是您真正需要开始的全部。您可能需要第三方库来开发您正在编写的任何类型的应用程序,但我想不出所有Python 程序员真正需要或想要的任何第三方模块。
Unlke the .NET/Windows programming world, there is no one set of dev tools that stands above all others. Different people use different editors a whole lot. In Python, a module namespace is fully within a single file and project organization is based on the filesystem, so people do not lean on their IDEs as hard. Different projects use different version control software, which has been booming with new faces recently. Most of these are better than TFS and all are 1000 times better than SourceSafe.
与 .NET/Windows 编程世界不同,没有一套开发工具能够超越所有其他工具。不同的人经常使用不同的编辑器。在 Python 中,模块命名空间完全在单个文件中,项目组织基于文件系统,因此人们不会那么依赖于他们的 IDE。不同的项目使用不同的版本控制软件,最近新面孔蓬勃发展。其中大部分都比 TFS 好,并且都比 SourceSafe 好 1000 倍。
When I want an interactive session, I use the vanilla Python interpreter. Various more fancy interpreters exist: bpython, ipython, IDLE. bpython is the least fancy of these and is supposed to be good about not doing weird stuff. ipython and IDLE can lead to strange bugs where code that works in them doens't work in normal Python and vice-versa; I've seen this first hand with IDLE.
当我想要一个交互式会话时,我使用 vanilla Python 解释器。存在各种更花哨的解释器:bpython、ipython、IDLE。bpython 是这些中最不喜欢的,并且应该擅长不做奇怪的事情。ipython 和 IDLE 会导致奇怪的错误,在这些错误中运行的代码在普通 Python 中不起作用,反之亦然;我已经在 IDLE 上亲眼目睹了这一点。
For some of the tools you asked about and some others
对于您询问的一些工具和其他一些工具
- In .NET you would use NUnit. In Python, use the stdlib
unittest
module. There are various third-party extensions and test runners, butunittest
should suit you okay.- If you really want to look into something beyond this, get
unittest2
, a backport of the 2.7 version ofunittest
. It has incorporated all the best things from the third-party tools and is really neat.
- If you really want to look into something beyond this, get
- In .NET you would use SQL Server. In Python, you may use PostgreSQL, MySQL, sqlite, or some other database. Python specifies a unified API for databases and porting from one to another typically goes pretty smoothly. sqlite is in the stdlib.
- There are various Object Relational Models to make using databases more abstracted. SQLAlchemy is the most notable of these.
- If you are doing network programming, get Twisted.
- If you are doing numerical math, get numpy and scipy.
- If you are doing web development, choose a framework. There are about 200000: Pylons, zope, Django, CherryPy, werkzeug...I won't bother starting an argument by recommending one. Most of these will happily work with various servers with a quick setting.
- If you want to do GUI development, there are quite a few Python bindings. The stdlib ships with Tk bindings I would not bother with. There are wx bindings (wxpython), GTK+ bindings (pygtk), and two sets of Qt bindings. If you want to do native Windows GUI development, get IronPython and do it in .NET. There are win32 bindings, but they'll make you want to pull your hair out trying to use them directly.
- 在 .NET 中,您将使用 NUnit。在 Python 中,使用 stdlib
unittest
模块。有各种第三方扩展和测试运行器,但unittest
应该适合你。- 如果您真的想研究更多内容,请获取
unittest2
2.7 版本的unittest
. 它整合了第三方工具中所有最好的东西,而且非常简洁。
- 如果您真的想研究更多内容,请获取
- 在 .NET 中,您将使用 SQL Server。在 Python 中,您可以使用 PostgreSQL、MySQL、sqlite 或其他一些数据库。Python 为数据库指定了统一的 API,从一个数据库移植到另一个数据库通常会非常顺利。sqlite 在标准库中。
- 有各种对象关系模型可以使数据库的使用更加抽象。SQLAlchemy 是其中最引人注目的。
- 如果您正在进行网络编程,请使用 Twisted。
- 如果您正在做数值数学,请使用 numpy 和 scipy。
- 如果您正在做 Web 开发,请选择一个框架。大约有 200000 个:Pylons、zope、Django、CherryPy、werkzeug……我不会费心推荐一个来开始争论。其中大多数可以通过快速设置与各种服务器一起愉快地工作。
- 如果你想做 GUI 开发,有相当多的 Python 绑定。stdlib 附带 Tk 绑定,我不会打扰。有 wx 绑定(wxpython)、GTK+ 绑定(pygtk)和两组 Qt 绑定。如果您想进行本地 Windows GUI 开发,请获取 IronPython 并在 .NET 中进行。有 win32 绑定,但它们会让你想直接使用它们。
回答by Tom Willis
In order to reduce the chance of effecting/hosing the system install of python, I typically install virtualenvon the ubuntu python install. I then create a virtualenv in my home directory so that subsequent packages I install via pip or easy_install do not effect the system installation. And I add the bin from that virtualenv to my path via .bashrc
为了减少影响/安装 python 系统安装的机会,我通常在 ubuntu python 安装上安装virtualenv。然后我在我的主目录中创建一个 virtualenv,以便我通过 pip 或 easy_install 安装的后续包不会影响系统安装。我通过.bashrc 将那个 virtualenv 中的 bin 添加到我的路径中
$ sudo apt-get install python-virtualenv
$ virtualenv --no-site-packages ~/local
$ PATH=~/local/bin:$PATH #<----- add this to .bashrc to make it permanent
$ easy_install virtualenv #<--- so that project environments are based off your local environment rather than the system, probably not necessary
Install your favorite editor, I like emacs + rope, but editors are a personal preference and there are plenty of choices.
安装你最喜欢的编辑器,我喜欢emacs+rope,但编辑器是个人喜好,有很多选择。
When I start a new project/idea I create a new virtual environment for that project, so that I don't effect dependencies anywhere else. Since I would hate for some of my projects to break due to an upgrade of a library both that project and the new one depends on.
当我开始一个新项目/想法时,我为该项目创建了一个新的虚拟环境,这样我就不会影响其他任何地方的依赖项。因为我不希望我的一些项目由于该项目和新项目所依赖的库的升级而中断。
~/projects $ virtualenv --no-site-packages my_new_project.env
~/projects/my_new_project.env $ source bin/activate
(my_new_project.env)~/projects/my_new_project.env $ easy_install paste ipython #whatever else I think I need
(my_new_project.env)~/projects/my_new_project.env $ emacs ./ & # start hacking
When creating a new package...in order to have something that will be easy_installable/pippable use paster create
当创建一个新的包时......为了有一些容易安装/pippable的东西,使用 paste create
(my_new_project.env)~/projects/my_new_project.env$ paster create new_package
(my_new_project.env)~/projects/my_new_project.env/new_package$ python setup.py develop new_package
That's the common stuff as far as I can think of it. Everything else would be editor/version control tool specific
这就是我能想到的常见的东西。其他一切都是特定于编辑器/版本控制工具的
回答by Eric Wilson
Since I'm accustomed to Eclipse, I find Eclipse + PyDev convenient for Python. For quick computations, Idle is great.
由于习惯了Eclipse,我觉得Eclipse+PyDev对Python来说很方便。对于快速计算,空闲非常好。
I've used Python on Windows and on Ubuntu, and Linux is much cleaner.
我在 Windows 和 Ubuntu 上使用过 Python,Linux 更简洁。
回答by Jorge Gajon
If you launch a terminal and type python
you'll get an interpreter, where you can start trying stuff.
如果你启动一个终端并输入python
你会得到一个解释器,你可以在那里开始尝试。
Just in case you haven't seen it, check out the book Dive Into Python, is free on-line. http://www.diveintopython.org/
以防万一您还没有看过它,请查看在线免费的Dive Into Python一书。 http://www.diveintopython.org/
Follow the examples in the book using the interpreter.
使用解释器按照书中的示例进行操作。
For storing your work you could use any editor; Vim or EMACS could be the most powerful, but also the most difficult to learn at first. If you want a more "traditional"IDE, you could try WingIDE. http://www.wingware.com/
为了存储您的工作,您可以使用任何编辑器;Vim 或 EMACS 可能是最强大的,但一开始也是最难学的。如果你想要一个更“传统”的IDE,你可以试试WingIDE。 http://www.wingware.com/
After you start to get more comfortable with python you should try an enhanced interpreter; try ipython
.
http://ipython.scipy.org/moin/
在你开始对 python 更加熟悉之后,你应该尝试一个增强的解释器;试试ipython
。
http://ipython.scipy.org/moin/
When you start to develop a more serious project you'll need to get additional modules. Here you have two options; 1) Use your distribution tools to install additional modules; or 2) Download the modules you need directly from their sites and install them manually. You'll be responsible to upgrade them of course.
当你开始开发一个更严肃的项目时,你需要获得额外的模块。在这里你有两个选择;1) 使用您的分发工具安装附加模块;或 2) 直接从他们的站点下载您需要的模块并手动安装它们。当然,您将负责升级它们。
You'll have to decide for yourself which way to go. Personally I prefer to download and install additional modules manually.
你必须自己决定走哪条路。我个人更喜欢手动下载和安装其他模块。
回答by Ignacio Vazquez-Abrams
Python (duh), setuptools or pip, virtualenv, and an editor. I suggest geany, but that's just me. And of course, any other Python modules you'll need.
Python (duh)、setuptools 或 pip、virtualenv 和编辑器。我建议geany,但那只是我。当然,还有您需要的任何其他 Python 模块。
回答by you cad sir - take that
Getting to Python from .NET world
从 .NET 世界开始使用 Python
Jumping into the Linux world from a .NET / WIndows background can be a bit disconcerting (but I do encourage you to keep trying Linux)
从 .NET / WINdows 背景进入 Linux 世界可能有点令人不安(但我鼓励你继续尝试 Linux)
But I would suggest to anyone coming from Windows, to stick with Windows for a little while. goto www.Activestate.com and download their Python package - it includes the full win32com extentions by Mark Hammond and it also includes a complete, fast IDE "pythonwin"
但我会建议任何来自 Windows 的人,坚持使用 Windows 一段时间。转到 www.Activestate.com 并下载他们的 Python 包 - 它包括 Mark Hammond 的完整 win32com 扩展,还包括一个完整、快速的 IDE“pythonwin”
I have done real professional development with just this setup alone on a windows box - one 14MB .msi and off you go !
我仅在 Windows 机器上仅使用此设置就完成了真正的专业开发 - 一个 14MB .msi,然后就可以了!
Now to use Python on the DLR (Dynamic common language runtime) you need to download IronPython. THis is a seperate interpreter, that was also originally written by Mark Hammond at Microsoft and is at ironpython.org.
现在要在 DLR(动态公共语言运行时)上使用 Python,您需要下载 IronPython。这是一个单独的解释器,最初也是由 Microsoft 的 Mark Hammond 编写的,位于 Ironpython.org。
With this you can run code like (from wikipedia) ::
有了这个,你可以运行代码(来自维基百科)::
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import MessageBox
MessageBox.Show("Hello World")
Now you can access any .NET code from python.
现在您可以从 python 访问任何 .NET 代码。
回答by S.Lott
You don't need much. Python comes with "Batteries Included."
你不需要太多。Python 附带“包含电池”。
Visual Studio == IDLE. You already have it. If you want more IDE-like environment, install Komodo Edit.
Visual Studio == 空闲。你已经拥有了。如果您想要更多类似 IDE 的环境,请安装 Komodo Edit。
NUnit == unittest
. You already have it in the standard library.
NUnit == unittest
。您已经在标准库中拥有它。
SQL Server == sqlite
. You already have it in the standard library.
SQL 服务器 == sqlite
。您已经在标准库中拥有它。
Stop wasting time getting everything ready. It's already there in the basic Python installation.
不要浪费时间准备好一切。它已经存在于基本的 Python 安装中。
Get to work.
开始工作。
Linux, BTW, isprimarily a development environment. It was designed and built by developers for developers. Windows is an end-user environment which has to be supplemented for development.
Linux的,顺便说一句,是主要开发环境。它是由开发人员为开发人员设计和构建的。Windows 是一个最终用户环境,必须对其进行补充以进行开发。
Linux was originally focused on developers. All the tools you need are either already there or are part of simple yum or RPM installs.
Linux 最初专注于开发人员。您需要的所有工具要么已经存在,要么是简单的 yum 或 RPM 安装的一部分。
回答by Will McCutchen
If you're just starting out with Python, I'd actually argue against bringing in the complexity of virtualenv
(which I think can be pretty overwhelming), at least until you've got a firm grasp of Python basics (especially regarding library/dependency management).
如果您刚开始使用 Python,我实际上反对引入 Python 的复杂性virtualenv
(我认为这可能非常令人难以抗拒),至少在您牢牢掌握 Python 基础知识之前(尤其是关于库/依赖项)管理)。
If you're using Ubuntu and the Gnome desktop environment, gedit
is the default (gui) text editor, and has great support for Python built in. So my recommendation is to start with the pre-installed Python and gedit
(which is pretty extensible on its own).
如果您使用的是 Ubuntu 和 Gnome 桌面环境,gedit
它是默认的 (gui) 文本编辑器,并且对内置的 Python 有很好的支持。所以我的建议是从预装的 Python 开始,并且gedit
(它在自己的)。
回答by Will McCutchen
You would probably like to give NetBeans Python IDEa shot. You can choose to use either Windows/Linux.
您可能想尝试一下NetBeans Python IDE。您可以选择使用 Windows/Linux。
回答by wisty
Database: sqlite (inbuilt). You might want SQLAlchemy though.
数据库:sqlite(内置)。不过,您可能需要 SQLAlchemy。
GUI: tcl is inbuilt, but wxPython or pyQt are recommended.
图形界面:tcl 是内置的,但推荐使用 wxPython 或 pyQt。
IDE: I use idle (inbuilt) on windows, TextMate on Mac, but you might like PyDev. I've also heard good things about ulipad.
IDE:我在 Windows 上使用 idle(内置),在 Mac 上使用 TextMate,但您可能喜欢 PyDev。我也听说过关于 ulipad 的好消息。
Numerics: numpy.
数字:麻木。
Fast inline code: lots of options. I like boost weave (part of scipy), but you could look into ctypes (to use dlls), Cython, etc.
快速内联代码:很多选项。我喜欢 boost weave(scipy 的一部分),但您可以查看 ctypes(使用 dll)、Cython 等。
Web server: too many options. Django (plus Apache) is the biggest.
Web 服务器:选项太多。Django(加上Apache)是最大的。
Unit testing: inbuilt.
单元测试:内置。
Pyparsing, just because.
Pyparsing,只是因为。
BeautifulSoup (or another good HTML parser).
BeautifulSoup(或其他优秀的 HTML 解析器)。
hg, git, or some other nice VC.
hg、git 或其他一些不错的 VC。
Trac, or another bug system.
Trac 或其他错误系统。
Oh, and StackOverflow if you have any questions.
哦,如果您有任何问题,还有 StackOverflow。