英文:
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> & C:/Python39/python.exe "c:/.../project/test/baz.py"
答案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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论