简体   繁体   English

从文件夹导入Python

[英]Python import from folder

I have such structure of application 我有这样的申请结构

mainApp.py
   |_Folder1
       |_fileFromFolder1.py
   |_Folder2
       |_fileFromFolder2.py

I want in fileFromFolder1.py import object from fileFromFolder2.py 我想要在fileFromFolder1.py中从fileFromFolder2.py导入对象

I know that one of solution is to sys.path.insert(0, '/path/to/application/app/folder') but is it possible to make without absolute path 我知道解决方案之一是sys.path.insert(0,'/ path / to / application / app / folder')但有可能没有绝对路径

You need(not strictly need in Python3, but still really really should have) __init__.py files in directories you would like to import code from. 您需要(在Python3中不是严格需要,但实际上确实应该有)要从中导入代码的目录中的__init__.py文件。 An __init__.py file makes the difference between a plain directory and a Python package __init__.py文件使纯目录和Python包有所不同

mainApp.py
   |_Folder1
       |__init__.py
       |_fileFromFolder1.py
   |_Folder2
       |__init__.py
       |_fileFromFolder2.py

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

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