没有找到模块 <module_name>

huangapple go评论74阅读模式
英文:

No module named <module_name>

问题

我有以下代码都在一个名为baz.py的文件中:

# foo.py
class Foo:
    def __init__(self, foo):
        self.foo = foo

# bar.py
class Bar:
    def __init__(self, bar):
        self.bar = bar
    # some functions

# baz.py
# 调用Bar中的一些函数

我想以以下方式组织我的项目:

project|-model|-foo.py
              |-bar.py
              |-__init__.py
       |-test |-baz.py
       |-__init__.py

并运行baz.py

为此,在baz.py的顶部添加了以下内容:

from project.model import foo, bar

然而,当使用以下命令时,我收到错误消息"找不到模块project":

C:\...\project> & C:/Python39/python.exe "c:/.../project/test/baz.py"

注意:在你的代码中,要使用import语句,确保目录结构和文件名的拼写与实际项目的结构一致。此外,确保Python解释器能够找到项目的根目录。

英文:

I have the following code all in a file named baz.py

# foo.py
class Foo:
    def __init__(self,foo):
        self.foo = foo

# bar.py
class Bar:
    def __init__(self,bar):
        self.bar = bar
    # some functions

# baz.py
# calling some functions from Bar

I would like to organize my project in the following manner:

project|-model|-foo.py
              |-bar.py
              |-__init__.py
       |-test |-baz.py
       |-__init__.py

And run baz.py

To do so I added at the top of baz.py the following

from project.model import foo, bar

However I get the error "No module named project" when using

C:\...\project&gt; &amp; C:/Python39/python.exe &quot;c:/.../project/test/baz.py&quot;

答案1

得分: 2

看起来你正在以project作为你的工作目录运行脚本,而Python在project目录内找不到名为project的目录。你尝试过将你的代码行修改为:

from model import foo, bar

因为model位于project目录内。或者你可以从project的父目录调用这个函数。

英文:

It seems that you're running the script with project as your working directory, and python does not find a directory named project inside of project. Have you tried to modify your line to:

from model import foo, bar

Since model is inside the project directory. Or you can call the function from the parent directory of project.

huangapple
  • 本文由 发表于 2023年3月15日 18:20:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定