python包中的脚本
我开发了 python 应用程序,我决定将其转换为软件包,以便稍后通过 easy_install
或 pip
安装。我使用搜索找到了一些有关 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要相对导入。尝试
或
文档在这里
http://docs.python.org/tutorial /modules.html#intra-package-references
这些适用于 Python 2.5 或更高版本。
要仅在开发版本中执行此操作,请尝试:
You need relative imports. Try
or
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:
您可以使用
virtualenv
在开发过程中测试 Python 代码,就像它一样被释放You can use
virtualenv
for testing Python code while in development as if it was released最简单的方法是配置正确的 Python 路径,以便 Python 知道在当前目录中查找
my_package
。在 Linux 上(使用 Bash):
在 Windows 上:
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):
On Windows:
现在有 console_scripts 方法。请参阅
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.
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.