gpt4 book ai didi

Import could not be resolved for modules in same folder(无法为同一文件夹中的模块解析导入)

转载 作者:bug小助手 更新时间:2023-10-25 15:08:33 25 4
gpt4 key购买 nike



I have two .py files (modules). I'm trying to import one into the other:

我有两个.py文件(模块)。我正在尝试将一个导入到另一个:


from place import Place

place is the name of the .py files and Place name of the class inside it. I am getting an error:

Place是.py文件的名称和其中的类的位置名称。我收到一个错误:



Import "place" could not be resolved Pylance(reportMissingImports)



I have Python installed correctly and modules are both in the same folder. Answers tried:

我已经正确安装了Python,并且两个模块都在同一个文件夹中。已尝试的答案:



file_to_do_import.py:

FILE_TO_DO_IMPORT.py:


from place import Place

place.py:

Place.py:


class Place:
def __init__(self, place_name, place_address, num_days, total_cost):
# Instance variables for each book instance!!!!
self.place_name = place_name
self.place_address = place_address
self.num_days = num_days
self.total_cost = total_cost

def __str__(self):
# Instance method to return book information as a string!!!!
return "{} by {}, published in {}".format(self.place_name, self.place_address, self.num_days, self.total_cost)

更多回答

You can probably ignore the Pylance error to begin with; does your program run successfully?

您可以从一开始就忽略Pylance错误;您的程序是否成功运行?

Ah sorry forgot to mention, no I cannot run the program. I get the following debug error Exception has occurred: ModuleNotFoundError No module named 'place'

啊,对不起,忘了说了,不,我不能运行这个程序。我得到以下调试错误异常:ModuleNotFoundError没有名为‘Place’的模块

Then I'd make extra double sure that you actually do have place.py. (You could try to create a new file called place.py; if that succeeds, then you didn't have one...)

我会加倍确保你真的有place.py (You可以尝试创建一个名为place.py的新文件;如果成功,那么您没有一个.)

I definitely do. "place.py" but I've just realised that the sub-folder has a spacing in the name, does that affect python when its trying to look for another file in its directory?

我当然想。但我刚刚意识到,子文件夹的名称中有一个空格,当它试图在其目录中查找另一个文件时,这会影响到它吗?

优秀答案推荐

2 options:

2个选项:


1). Add a __init__.py in your directory to make your directory a module.

1)。在您的目录中添加__init__.py,使您的目录成为一个模块。


2). Use from .place import Place

2)。从.place导入位置使用


更多回答

neither seem to work for me

这两个似乎都不适合我

So, you have a project directory with 3 files: __ init __.py, place.py, file_to_do_import.py? In your file_to_do_import.py, you from .place import Place, and you get the import error? Check that your 'class Place(object)' is not indented at all or inside another class?

那么,您有一个包含3个文件的项目目录:__init__.py、place.py、file_to_do_port.py?在您的_to_do_port.py文件中,您来自.place导入位置,并且收到导入错误?检查您的“类位置(对象)”是否完全没有缩进或在另一个类中?

ok yep so I've got: init.py, place.py, file_to_do_import.py and I've added the "." to place. Now I'm getting the same issue in development, but, the following error when I try to run it in debug: Exception has occurred: ImportError attempted relative import with no known parent package. Also there's no indent in class object.

好的,是的,所以我有:init.py,place.py,file_to_do_port.py,我已经添加了“。放到一个地方。现在我在开发中遇到了同样的问题,但是,当我尝试在调试中运行它时出现以下错误:发生异常:ImportError尝试在没有已知父包的情况下进行相对导入。此外,类对象中也没有缩进。

I've added in my code to the original question (how the code was before I asked the question).

我已经在我的代码中添加了原始问题(在我提出问题之前,代码是如何的)。

It sounds like python is being executed outside of this path, and doesn't know where that module lives. You could try to use the parent directory as the package name, for example: from parentDirectory.place import Place. That could work, but chances are, python doesn't know of that module. If you are executing python manually, you'll want to add that project path to python. i.e. python -m pathToProjectDir <other params>

听起来像是在此路径之外执行了python,并且不知道该模块驻留在哪里。您可以尝试使用父目录作为包名,例如:from parentDirectory.place导入位置。这可能会起作用,但很有可能,Python并不知道这个模块。如果您正在手动执行python,则需要将该项目路径添加到python中。即python-m pathToProjectDir

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com