简体   繁体   中英

How to load python module from a neighbouring package?

I've made two python packages as follows:

theMainFolder/
├── package_a/
│   ├── __init__.py
│   └── some_a_file.py
├──package_b/
│   ├── __init__.py
│   └── some_b_file.py

I now want to import some_a_file into some_b_file. I tried doing this using:

from package_a import some_a_file

but this doesn't work. Does anybody know how I can do this? All tips are welcome.

运行程序时,将theMainFolder的路径添加到模块搜索路径:

PYTHONPATH=/path/to/theMainFolder python main_program.py

Relatively add path to the location of theMainFolder and then do an import

import sys                                               
from os.path import dirname, abspath                     
sys.path.insert(0, dirname(dirname(abspath(__file__))))  
from package_a import some_a_file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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