简体   繁体   English

来自不同子目录的Python 3.6导入方法

[英]Python 3.6 import methods from different subdirectories

I know there are a lot of readings for this topic available, but I can't figure out how to get it work for my specific case. 我知道该主题有很多读物,但是我不知道如何使它适用于我的特定情况。

I have a project structure like this 我有一个这样的项目结构

root
    folder1
          __init__.py
          file1.py
          file2.py
    folder2
          __init__.py
          file3.py
          file4.py
          folder3
                 __init__.py
                 file5.py

In file3.py I have a function getNumbers() which i want to import in file2.py like this: file3.py我有一个函数getNumbers() ,我想将其导入到file2.py如下所示:

from folder2.file3 import getNumbers()

The __init__.py files are all empty. __init__.py文件都是空的。

I do get Error if I run file2.py from folder1 -directory 如果我从folder1 -directory运行file2.py ,我会得到错误

No module named 'folder2'

How can i get the import from different subdirectories work? 如何从不同的子目录导入工作?

maybe use this 也许用这个

# some_file.py 
import sys 
sys.path.insert(0, '/path/to/application/app/folder') 

you may also use path.append(...) , for full answer see this link: Importing files from different folder 您也可以使用path.append(...) ,有关完整答案,请参见以下链接: 从其他文件夹导入文件

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

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