简体   繁体   English

从另一个文件夹导入 Python Package

[英]Importing Python Package from another folder

I have seen many questions and answers for this same, but none of them solved mine.我已经看到了很多同样的问题和答案,但没有一个能解决我的问题。 Please have a look into this,请看看这个,

This is my folder structure,这是我的文件夹结构,

code/ 
  helloworld.py
stdlib/
  stdio.py
  stddraw.py 
  __init__.py

Inside __init.py i have done在 __init.py 里面我已经完成了

from . import stdio

Inside helloworld.py, i have done在 helloworld.py 里面,我已经完成了

from ..stdlib import stdio
stdio.writeln("Test")

But it says me an error saying, " attempted relative import with no known parent package " Can anyone please explain me what i am doing wrong in this case?但它告诉我一个错误,“尝试在没有已知父级 package 的情况下进行相对导入” 谁能解释我在这种情况下做错了什么?

As the error says, you would need a "parent package" for what you're currently doing to work.正如错误所说,你需要一个“父包”来完成你目前正在做的工作。 Something like this:像这样的东西:

parent/
 __init__.py
 code/ 
  helloworld.py
 stdlib/
  stdio.py
  stddraw.py 
  __init__.py

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

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