python包中的脚本

发布于 2024-11-26 11:27:33 字数 1625 浏览 1 评论 0原文

我开发了 python 应用程序,我决定将其转换为软件包,以便稍后通过 easy_installpip 安装。我使用搜索找到了一些有关 python 包目录结构的好资料请参阅 这个答案这篇文章

我创建了以下结构(我省略了列表中的几个文件以使结构更清晰)

Project/
|-- bin/
|-- my_package/
|   |-- test/
|   |   |-- __init__.py
|   |   |-- test_server.py
|   |-- __init__.py
|   |-- server.py
|   |-- util.py
|-- doc/
|   |-- index.rst
|-- README.txt
|-- LICENSE.txt
|-- setup.py           

之后我创建了可执行脚本 server-run

#!/usr/bin/env python
from my_package import server
    
server.main()

并将其放入 bin 目录中。如果我使用 python setup.py install 或通过 pip/easy_install 安装我的包,一切正常,我可以运行 server-run 和我的服务器开始处理传入的请求。

但我的问题是如何测试 server-run 在开发环境中工作(没有事先安装 my_package)?另外,我想使用此脚本来运行最新的服务器代码以用于开发目的。

开发发生在 Project 目录中,因此如果我运行 ./bin/server-run ,我会收到 ImportError

user@host:~/dev/Project/$ ./bin/server-run
Traceback (most recent call last):
  File "./bin/server-run", line 2, in <module>
    import my_package
ImportError: No module named my_package

是否可以修改 bin /server-run 脚本,因此如果我从文件系统中的另一个文件夹(不一定是从 Project 目录)运行它,它会起作用吗?另请注意,我想使用(如果可以实现)相同的脚本在生产环境中运行服务器。

I develop python application which I decided to turn into package to be installed by easy_install or pip later. I've used search to find several good sources about directory structure for python packages See this answer or this post.

I created following structure (I've omitted several files in the list to make structure be more clear)

Project/
|-- bin/
|-- my_package/
|   |-- test/
|   |   |-- __init__.py
|   |   |-- test_server.py
|   |-- __init__.py
|   |-- server.py
|   |-- util.py
|-- doc/
|   |-- index.rst
|-- README.txt
|-- LICENSE.txt
|-- setup.py           

After that I created executable script server-run

#!/usr/bin/env python
from my_package import server
    
server.main()

which I placed into bin directory. If I install my package with python setup.py install or via pip/easy_install everything works fine, I can run server-run and my server starts to handle incoming requests.

But my question is how to test that server-run works in development environment (without prior installation of my_package)? Also I want to use this script to run latest server code for dev purposes.

Development happens in Project directory so I am getting ImportError if I run ./bin/server-run

user@host:~/dev/Project/$ ./bin/server-run
Traceback (most recent call last):
  File "./bin/server-run", line 2, in <module>
    import my_package
ImportError: No module named my_package

Is it possible to modify bin/server-run script so it will work if I run it from another folder somewhere in the filesystem (not necessarily from Project dir)? Also note that I want to use (if it is possible to achieve) the same script to run server in production environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断肠人 2024-12-03 11:27:33

您需要相对导入。尝试

from .. import mypackage

from ..mypackage import server

文档在这里

http://docs.python.org/tutorial /modules.html#intra-package-references

这些适用于 Python 2.5 或更高版本。

要仅在开发版本中执行此操作,请尝试:

try:
    from my_package import server
except ImportError:
    from ..my_package import server

You need relative imports. Try

from .. import mypackage

or

from ..mypackage import server

The documentation is here

http://docs.python.org/tutorial/modules.html#intra-package-references

These work on Python 2.5 or newer.

To do it only in the development version, try:

try:
    from my_package import server
except ImportError:
    from ..my_package import server
眉目亦如画i 2024-12-03 11:27:33

您可以使用 virtualenv 在开发过程中测试 Python 代码,就像它一样被释放

You can use virtualenv for testing Python code while in development as if it was released

¢好甜 2024-12-03 11:27:33

最简单的方法是配置正确的 Python 路径,以便 Python 知道在当前目录中查找 my_package

在 Linux 上(使用 Bash):

export PYTHONPATH=.
bin/server-run

在 Windows 上:

set PYTHONPATH=.
python bin/server-run

The simplest way is to configure the right Python path, so Python knows to look for my_package in the current directory.

On Linux (using Bash):

export PYTHONPATH=.
bin/server-run

On Windows:

set PYTHONPATH=.
python bin/server-run
剩余の解释 2024-12-03 11:27:33

现在有 console_scripts 方法。请参阅

entry_points={
      'console_scripts': [
        'wikibackup = wikibot.wikipush:mainBackup',   
        'wikiedit = wikibot.wikipush:mainEdit',
        'wikinuke = wikibot.wikipush:mainNuke',
        'wikipush = wikibot.wikipush:mainPush',
        'wikiupload = wikibot.wikipush:mainUpload',
        'wikiuser = wikibot.wikiuser:main',
      ],
    },

https://pypi.org/project/py-3rdparty-mediawiki/ (我是提交者)。

如果您对该软件包进行 pip 安装,上述脚本将作为安装过程的一部分进行安装。

请参阅 https://github.com/WolfgangFahl/py-3rdparty -mediawiki/blob/master/setup.py 获取安装脚本的完整源代码。

There is the console_scripts approach now. See e.g.

entry_points={
      'console_scripts': [
        'wikibackup = wikibot.wikipush:mainBackup',   
        'wikiedit = wikibot.wikipush:mainEdit',
        'wikinuke = wikibot.wikipush:mainNuke',
        'wikipush = wikibot.wikipush:mainPush',
        'wikiupload = wikibot.wikipush:mainUpload',
        'wikiuser = wikibot.wikiuser:main',
      ],
    },

from https://pypi.org/project/py-3rdparty-mediawiki/ (where i am a committer).

If you do a pip install of that package the above scripts will be installed as part of the installation process.

see https://github.com/WolfgangFahl/py-3rdparty-mediawiki/blob/master/setup.py for the full source code of the setup script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文