使用 python 包在“<packageName>”中接收到“找不到 __main__”模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24723547/
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
Received 'can't find '__main__' module in '<packageName>' with python package
提问by OpenDataAlex
I'm trying to release my first Python package in the wild and I was successful in setting it up on PyPi and able to do a pip install
. When I try to run the package via the command line ($ python etlTest
), I receive the following error:
我正在尝试在野外发布我的第一个 Python 包,并且我成功地在 PyPi 上设置了它并且能够执行pip install
. 当我尝试通过命令行 ( $ python etlTest
)运行包时,收到以下错误:
/usr/bin/python: can't find '__main__' module in 'etlTest'
/usr/bin/python: can't find '__main__' module in 'etlTest'
When I run the code directly from my IDE, it works without issue. I am using Python 2.7 and have __init__.py
scripts where required. What do I need to do to get this working?
当我直接从我的 IDE 运行代码时,它可以正常工作。我正在使用 Python 2.7 并__init__.py
在需要时使用脚本。我需要做什么才能让它发挥作用?
采纳答案by famousgarkin
I can easily replicate your problem, actually even without using your package:
我可以轻松地复制您的问题,实际上即使不使用您的包:
$ python empty
.env/bin/python: can't open file 'empty': [Errno 2] No such file or directory
$ mkdir empty
$ python empty
.env/bin/python: can't find '__main__' module in 'empty'
$ python Empty
.env/bin/python: can't find '__main__' module in 'Empty'
So you are not calling your library at all, you are just giving the Python interpreter a nonexistent script name, which in case there is a like-named directory (case-insensitive even) in the working directory it tries to execute it.
所以你根本没有调用你的库,你只是给 Python 解释器一个不存在的脚本名称,以防它试图执行它的工作目录中有一个类似命名的目录(甚至不区分大小写)。
I was able to install your package from PyPi just fine and can import it alright, but there doesn't seem to be any entry pointto it, nothing useful is exported at the top level and no executable scripts are added to Python bin:
我能够从 PyPi 安装你的包就好了,可以导入它,但似乎没有任何入口点,在顶层没有任何有用的东西被导出,并且没有可执行脚本添加到 Python bin:
>>> import etltest
>>> dir(etltest)
['__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__path__', '__version__']
Running the etlTest.py directly as suggested in Quickstart from your docsdoesn't work either:
按照文档中Quickstart 中的建议直接运行 etlTest.py也不起作用:
$ python .env/lib/python2.7/site-packages/etltest/etlTest.py
2014-07-13 17:19:56,831 - settings - DEBUG - Attempting to load .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - No such file or directory .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - DEBUG - Attempting to load .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - No such file or directory .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - Could not find settings file in .env/lib/python2.7/site-packages/.etltest-settings.yml,.env/lib/python2.7/site-packages/.etltest-settings.yml. Using defaults where present.
Traceback (most recent call last):
File ".env/lib/python2.7/site-packages/etltest/etlTest.py", line 73, in <module>
main(sys.argv[1:])
File ".env/lib/python2.7/site-packages/etltest/etlTest.py", line 22, in main
SettingsManager().first_run_test()
File ".env/lib/python2.7/site-packages/etltest/utilities/settings_manager.py", line 29, in __init__
self.app_name = etltest_config['app_name']
KeyError: 'app_name'
I'd say your package is not apt for ditribution yet. You might want to clean up some dependencies on your development environment and read upon setuptools
entry pointsto provide proper command line executables along with your package. Also it shouldn't run in debug mode by default if it's not some kind of a testing release.
我会说你的包裹还不适合分发。您可能希望清除对开发环境的一些依赖,并阅读setuptools
入口点以提供适当的命令行可执行文件以及您的包。此外,如果它不是某种测试版本,则默认情况下它不应在调试模式下运行。
回答by Lbrth_BoC
I recently got the same issue and finaly find the solution by my self.
So as the error said, I added a file __main__.py
at the same path that my __init__.py
.
我最近遇到了同样的问题,最后自己找到了解决方案。正如错误所说,我__main__.py
在与我的__init__.py
.
Inside __main__.py
I added the following code :
在里面__main__.py
我添加了以下代码:
from mypackage.mymodule import main
main()
main()
was the main function of my code. And it's works now.
main()
是我的代码的主要功能。它现在有效。
here my directory:
这是我的目录:
package
|__dirpackage
|_mypackage.py
|_ __init__.py
|_ __main__.py
|_setup.py`
回答by himanshu choudhary
Just change the name __init__.py file to __main__.py
只需将名称 __init__.py 文件更改为 __main__.py