简体   繁体   English

在本地安装 python 包进行开发的最佳方式

[英]Best way to install python packages locally for development

Being new to the python games I seem to have missed out on some knowledge on how you can develop on a program but also keep it in your live environment.作为 Python 游戏的新手,我似乎错过了一些关于如何在程序上进行开发同时将其保留在您的实时环境中的知识。

Programs like gpodder can be run directly from the source checkout which is really handy however others want to be "installed" to run.像 gpodder 这样的程序可以直接从源代码检出运行,这非常方便,但其他程序希望“安装”运行。

A lot of programs are distributed with a setup.py with instructions to run "python ./setup.py install" as root which will put stuff somewhere in your file-system.许多程序都与 setup.py 一起分发,其中包含以 root 身份运行“python ./setup.py install”的说明,这会将内容放在文件系统中的某个位置。 There are even install commands like "develop" which seem to hold the promise of what I want.甚至还有像“develop”这样的安装命令,它们似乎符合我想要的承诺。 So I tried:所以我试过:

export PYTHONPATH=/home/alex/python
python ./setup.py develop --install-dir=/home/alex/python

Which downloaded a bunch of stuff locally and seems magically ensure the application I'm hacking on is still being run out of the src tree.它在本地下载了一堆东西,似乎神奇地确保了我正在攻击的应用程序仍在 src 树中运行。 So I guess my roundabout question is is this the correct way of developing python code?所以我想我的迂回问题是这是开发python代码的正确方法吗? How do things like easy_install and pip factor into this? easy_install 和 pip 之类的东西是如何影响到这个的?

So I tried the following:所以我尝试了以下方法:

 python /usr/share/pyshared/virtualenv.py /home/alex/src/goobook
 cd /home/alex/src/goobook/googbook.git
 /home/alex/src/goobook/bin/python ./setup.py develop

And finally linked the program in question to my ~/bin最后将有问题的程序链接到我的 ~/bin

 cd /home/alex/src/goobook
 linkbin.pl bin/goobook

However invocation throws up a load of extra chatter which seems to imply it's wrong:然而,调用会引发大量额外的喋喋不休,这似乎暗示它是错误的:


17:17 alex@socrates/i686 [goobook] >goobook --help
/home/alex/bin/goobook:5: UserWarning: Module pkg_resources was already imported from        /home/alex/src/goobook/lib/python2.5/site-packages/setuptools-0.6c8-py2.5.egg/pkg_resources.py, but /home/alex/src/goobook/lib/python2.5/site-packages/distribute-0.6.10-py2.5.egg is being added to sys.path
  from pkg_resources import load_entry_point
/home/alex/bin/goobook:5: UserWarning: Module site was already imported from /home/alex/src/goobook/lib/python2.5/site.pyc, but /home/alex/src/goobook/lib/python2.5/site-packages/distribute-0.6.10-py2.5.egg is being added to sys.path
  from pkg_resources import load_entry_point

Install:安装:

http://pypi.python.org/pypi/virtualenv http://pypi.python.org/pypi/virtualenv

to set up a localized virtual environment for your libraries, and:为您的图书馆设置本地化的虚拟环境,以及:

http://pypi.python.org/pypi/setuptools http://pypi.python.org/pypi/setuptools

ie "easy_install" to install new things.即“easy_install”安装新东西。

Virtualenv allows you to work in completely independent and isolated Python environments. Virtualenv允许您在完全独立和隔离的 Python 环境中工作。 It will let you easily create multiple environments which have different Python packages installed or different versions of a same package.它将让您轻松创建多个环境,这些环境安装了不同的 Python 包或同一包的不同版本。 Virtualenv also lets you easily switch between your different environments. Virtualenv 还可以让您轻松地在不同的环境之间切换。

As of 2012, the de facto preferred tool for package management in Python is pip rather than setuptools.截至 2012 年,Python 中包管理的事实上的首选工具是pip而不是 setuptools。 Pip is able to handle dependencies and to install/uninstall globally or inside a virtual environment. Pip 能够处理依赖关系并在全局或虚拟环境中安装/卸载。 Pip even comes out-of-the-box with virtualenv. Pip 甚至可以通过 virtualenv 开箱即用。

Python 3蟒蛇 3

Also worth mentioning is the fact that virtual environments are becoming a part of Python itself in release 3.3 , with the implementation of PEP 405 .另外值得一提的是,随着PEP 405的实施,虚拟环境在3.3 版中成为 Python 本身的一部分。

The Python Packaging User Guide , which "aims to be the authoritative resource on how to package, publish and install Python distributions using current tools", recommends using pip to install in "development mode": Python 打包用户指南》“旨在成为有关如何使用当前工具打包、发布和安装 Python 发行版的权威资源”,建议使用 pip 在“开发模式”下进行安装:

pip install -e <path>

Thus in the root directory of your package you can simply因此,在包的根目录中,您可以简单地

pip install -e .

See installing from a local source tree .请参阅从本地源树安装

The best way to develop Python apps with dependencies is to:开发具有依赖项的 Python 应用程序的最佳方法是:

  1. Download desired version of the python interpreter.下载所需版本的 python 解释器。

  2. Install and use buildout ( http://www.buildout.org/ ).安装和使用 buildout ( http://www.buildout.org/ )。

Buildout is something like Maven for Java (will fetch all needed packages automatically). Buildout 类似于 Java 的 Maven(将自动获取所有需要的包)。

This way your Python interpreter will not be polluted by third party packages (this is important if you will be running developed application on other machines).这样你的 Python 解释器就不会被第三方包污染(如果你将在其他机器上运行开发的应用程序,这很重要)。 Additionally you can integrate buildout with virtualenv package (this allows you to create virtual python interpreters for each project).此外,您可以将 buildout 与 virtualenv 包集成(这允许您为每个项目创建虚拟 python 解释器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM