[英]ValueError: attempted relative import beyond top-level package python
I have a directory structure like this.我有一个这样的目录结构。
Chatbot/
utils/
abc.py
projects/
proj1/
utils/
__init__.py
data_process.py
components/
class1.py
I have two utils
folders in my structure, one at the top level and one inside my projects folder.我的结构中有两个utils
文件夹,一个在顶层,一个在我的项目文件夹中。
Now I want to import data_process.py
file inside class1.py
.现在我想在class1.py
导入data_process.py
文件。 So I tried like this所以我试过这样
from utils.data_process import DataProcess
but it is referencing the top-level utils
folder and even VSCode is not recognizing it.但它引用了顶级utils
文件夹,甚至 VSCode 也无法识别它。 I tried creating __init__.py
file inside the utils
folder but still did not work.我尝试在utils
文件夹中创建__init__.py
文件,但仍然无效。
I tried with empty __init__.py
and then placing this content我尝试使用空的__init__.py
然后放置此内容
from . import data_process
__all__ = ['data_proces']
then然后
from .data_process import DataPreprocess
__all__ = ['DataPreprocess']
then I tried然后我试过了
from ..utils.data_process import DataProcess
VSCode is recognizing this but it is not working and throws the error VSCode 正在识别这一点,但它无法正常工作并引发错误
ValueError: attempted relative import beyond top-level package
Even I tried changing the name utils to some other name but still the same issue即使我尝试将名称 utils 更改为其他名称,但仍然存在相同的问题
How can I solve this?我该如何解决这个问题?
A python module is defined by a special file called __init__.py
and this file should be present in all the subdirectories. python 模块由一个名为__init__.py
的特殊文件定义,该文件应存在于所有子目录中。 So, your file structure would be:因此,您的文件结构将是:
Chatbot/
__init__.py
utils/
__init__.py
abc.py
projects/
__init__.py
proj1/
__init__.py
utils/
__init__.py
data_process.py
components/
__init__.py
class1.py
class2.py
Now, you can do a relative import like:现在,您可以进行相对导入,例如:
.
使用.
for importing something within the same directory.用于导入同一目录中的内容。 Example:例子:# file Chatbot/projects/proj1/components/class2.py
from .class1 import *
..
for two-level, and ...
for three-level, and so on!同样,使用..
表示二级,使用...
表示三级,依此类推!For instance:例如:
# file Chatbot/projects/proj1/components/class2.py
from ..utils.data_process import * # import from data_process.py
# there will be four "." earlier I made a counting mistake
from ....utils.abc import * # import something from abc.py
When writing a python module/package, you might want to follow PEP8 Convention .在编写 python 模块/包时,您可能需要遵循PEP8 约定。
Moreover, I believe you are trying to do different projects using your package Chatbot
, is that correct?此外,我相信您正在尝试使用您的包Chatbot
来做不同的项目,对吗? Then, it is a good practice that you set PYTHONPATH
for Chatbot
and do all the projects, and imports seperately.然后,最好为Chatbot
设置PYTHONPATH
并执行所有项目,然后单独导入。
I'm able to work on this project seamlessly even with using two utils
directories.即使使用两个utils
目录,我也能够无缝地处理这个项目。 Project Structure:项目结构:
main
file and its output: main
文件及其输出:
# file main.py
from chatbot.utils.abc import hello as a1
from chatbot.projects.proj1.components.class1 import super_hello
from chatbot.projects.proj1.utils.data_process import hello as b1
print(a1(), b1())
print(super_hello())
Similarly, if you have chatbot
under PYTHONPATH
you can call the project from anywhere from your device.同样,如果您在PYTHONPATH
下有chatbot
,您可以从您的设备的任何位置调用该项目。
Code details and files are added into my github account for details.代码详细信息和文件添加到我的github帐户中以获取详细信息。
sys.path
is where Python searches to find modules and packages and It does it in order . sys.path
是 Python 搜索模块和包的地方,它按顺序执行。
So you can put ...../proj1/
at the beginning of the list, when python start searching it will find the utils
folder in that path first !所以你可以将...../proj1/
放在列表的开头,当python开始搜索时,它会首先找到该路径中的utils
文件夹!
import sys
sys.path.insert(0, r'...../proj1/')
But this cause another problem, python always find utils
in that folder first, that's not what you want.但这会导致另一个问题,python 总是先在该文件夹中查找utils
,这不是您想要的。
Absolute import:绝对导入:
from Chatbot.projects.proj1.utils.data_process import DataProcess
use relative imports which is mentioned by @Mr.Hobo.使用@Mr.Hobo 提到的相对导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.