python -m 找不到我的模块 [英] python -m cannot find my modules

查看:94
本文介绍了python -m 找不到我的模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个包,它位于 /home/michael/python/mcdb-mail-parser/

里面的文件结构是:

 .├── __init__.py├── mcdb_mail_parser│ ├── __init__.py│ ├── MCDBattachment.py│ ├── MCDBEmail.py│ ├── Options.py├── mcdb-mail-parser.conf.sample├── mcdb-mail-parser.py├── README.md

mcdb-mail-parser.pymcdb_mail_parser 子目录导入.

如果我从源目录 (/home/michael/python/src/mcdb_mail_parser) 运行脚本,它工作正常,因为 mcdb_mail_parser 目录在当前目录.但是,我需要从另一个用户的主目录(通过 cronjob,或通过子进程从另一个脚本)运行它,python 抱怨它找不到模块:

我试图用 python3 -m/home/michael/python/src/mcdb_mail_parser 执行它,但它抱怨:

michael@d8:~$ python3 -m/home/michael/python/mcdb-mail-parser//usr/bin/python3: 没有名为/home/michael/python/mcdb-mail-parser/的模块

我不知道从哪里开始.我认为这是一个路径问题.我可以将 /home/michael/python/src/mcdb_mail_parser 添加到系统路径,或者可能是 python 路径,但这似乎是错误的解决方案.我当然也不想将路径硬编码到任何脚本中.

我如何告诉 python:从 /home/michael/python/src/mcdb_mail_parser 目录运行 mcdb-mail-parser.py 脚本?

结束语接受的答案很有用,他们提供的链接也很有用.这是我最终做的:1. 我将 mcdb_mail_parser 的内容从子目录移动到与 README.md 相同的目录中,从而消除了一级复杂性.2. 我按照建议将导入语句添加到 __init__.py 中.3. Python 抱怨找不到__main__.py,所以我把mcdb-mail-parser.py 改名为__main__.py

解决方案

列出子目录中 __init__.py 中的模块,然后将 Import 放入mcdb-mail-parser.py 引用那个目录

与之前的 StackOverflow Post 非常相似 ->在 Python 中加载文件夹中的所有模块>

I wrote a package, which lives in /home/michael/python/mcdb-mail-parser/

The file structure in there is:

 .
 ├── __init__.py
 ├── mcdb_mail_parser
 │   ├── __init__.py
 │   ├── MCDBAttachment.py
 │   ├── MCDBEmail.py
 │   ├── Options.py
 ├── mcdb-mail-parser.conf.sample
 ├── mcdb-mail-parser.py
 ├── README.md

mcdb-mail-parser.py imports from the mcdb_mail_parser subdirectory.

if I run the scripts from the source directory (/home/michael/python/src/mcdb_mail_parser) it works fine because the mcdb_mail_parser directory is immediately available in the current directory. However, I need to run it from the home directory of another user (via a cronjob, or from another script via subprocess), python complains it cannot find the module:

I tried to execute it with python3 -m /home/michael/python/src/mcdb_mail_parser, but it complains:

michael@d8:~$ python3 -m /home/michael/python/mcdb-mail-parser/
/usr/bin/python3: No module named /home/michael/python/mcdb-mail-parser/

I am not sure where to go from here. I think that it's a path issue. I could add /home/michael/python/src/mcdb_mail_parser to the system path, or perhaps python path, but that seems like the wrong solution. I certainly don't want to hard code paths into any scripts either.

How do I tell python: "Run the mcdb-mail-parser.py script from /home/michael/python/src/mcdb_mail_parser directory?

Closing notes The accepted answer was useful, and so was the link they provided. Here's what I eventually did: 1. I moved the contents of mcdb_mail_parser from the subdirectory into the same directory as README.md, thus removing one level of complexity. 2. I added the import statements to __init__.py as suggested. 3. Python complained it couldn't find __main__.py, so I renamed mcdb-mail-parser.py to __main__.py

解决方案

List the modules in the __init__.py that's in the sub directory and then have the Import in mcdb-mail-parser.py reference that directory

Very similar to this previous StackOverflow Post -> Loading all modules in a folder in Python

这篇关于python -m 找不到我的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆