简体   繁体   English

是什么导致导入错误:“没有名为模块的模块”?

[英]What is causing Import Error: "no module named modules"?

I am working on my first open source project.我正在开发我的第一个开源项目。

While getting everything setup for the project, I followed the README.md step by step.在为项目进行所有设置的同时,我一步一步地按照 README.md 进行操作。

Now, I run into a problem.现在,我遇到了一个问题。 When I try to run a test and src scripts, I get the following error,当我尝试运行测试和 src 脚本时,出现以下错误,

ImportError: No module named modules

Now, below is the file structure.现在,下面是文件结构。

../
   /modules
        __init__.py
        /src
            lyrics.py 
        /tests
            test_lyrics.py 

lyrics.py import statements歌词.py 导入语句

import modules

def test_lyrics():
    assert('lyrics' == modules.process_query('paradise lyrics')[0])

This is where the error "Import Error: modules not found".这是错误“导入错误:未找到模块”的地方。

Yes, all the requirements on the README were met.是的,README 中的所有要求都已满足。

If you want to take a look at the project, check it out on github .如果您想查看该项目,请在 github 上查看。

It could be that your module's directory is not being read by your PYTHONPATH.可能是您的 PYTHONPATH 未读取您的模块目录。 To check this go to your home directory and look for a .bashrc or some kind of .profile file.要检查这一点,请转到您的主目录并查找.bashrc或某种.profile文件。 You may have to use ls -a to see these hidden files.您可能必须使用ls -a才能查看这些隐藏文件。 If you don't see your module's address listed, add this to the file:如果您没有看到列出的模块地址,请将其添加到文件中:

export PYTHONPATH="${PYTHONPATH}:/my/module/path"

Make sure the address points to the directory with your highest-level __init__.py file as python uses it to read the directory as a python package.确保地址指向具有最高级别__init__.py文件的目录,因为 python 使用它将目录作为 python 包读取。

Use python -m site to conveniently view the effect of the $PYTHONPATH env var on sys.path .使用python -m site可以方便地查看 $PYTHONPATH 环境变量对sys.path的影响。

Code authors will often assume that current directory is in your path:代码作者通常会假设当前目录在您的路径中:

$ export PYTHONPATH=.

If you intend to use .如果您打算使用. current directory, then pay careful attention to which one's in use at the time the script starts running.当前目录,然后注意脚本开始运行时正在使用的目录。 You may want to view this value: os.getcwd()你可能想查看这个值: os.getcwd()

I am maybe a bit late to the party but I can answer and maybe it helps.我参加聚会可能有点晚了,但我可以回答,也许会有所帮助。

1)Check once more your paths. 1)再次检查你的路径。 2)Check that you run with >./myProj arguments more arguments, and not 2)检查您是否使用 >./myProj 参数运行更多参数,而不是

myProj arguments more arguments. myProj 参数更多参数。 Hope it helps even a bit希望它会有所帮助

You could try using a relative import您可以尝试使用相对导入

from ..modules import process_query
def test_lyrics():
    assert('lyrics' == process_query('paradise lyrics')[0])

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

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