程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python relative Import script two levels up

編輯:Python

problem :

I've been struggling with imports in my package for the last hour. In the last hour , I have been struggling for the import in my bag .

I've got a directory structure like so: I have a directory structure like this :

main_package | | __init__.py | folder_1 | | __init__.py | | folder_2 | | | __init__.py | | | script_a.py | | | script_b.py | | folder_3 | | __init__.py | | script_c.py

I want to access code in script_b.py as well as code from script_c.py from script_a.py . I want to visit script_b.py Code in and from script_c.py Of script_a.py Code .How can I do this? What should I do ?

If I put a simple import script_b inside script_a.py , when I run If I were in script_a.py Put a simple import script_bscript_a.py , When I run

from main_package.folder_1.folder_2 import script_b

I am met with an I met a

ImportError: no module named "script_b"

For accessing script_c.py , I have no clue. For the visit script_c.py , I don't know .I wasn't able to find any information about accessing files two levels up, but I know I can import files one level up with I can't find any information about accessing the upper two levels of files , But I know that I can import a higher level file

from .. import some_module

How can I access both these files from script_a.py ? How to go from script_a.py Access these two files ?


Solution :

Reference resources : https://stackoom.com/en/question/2UWLH

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved