简体   繁体   English

Python:如何从任何位置导入用户定义的包?

[英]Python : How to import user defined packages from any location?

I am trying to import packages in python . 我正在尝试在python中导入包。 Below mentioned is my folder structure 下面提到的是我的文件夹结构

MainFolder
      |config| locator|logs|page|tests

Each folder has __init__.py file . 每个文件夹都有__init__.py文件。 tests folder has test.py . tests文件夹包含test.py Now , inside test.py i am trying to import files from folders - config| 现在,在test.py我尝试从文件夹-config |中导入文件。 locator|logs 定位器|日志
However python is not recognizing them as package. 但是python无法将它们识别为包。 If I place them under C:\\Python27\\Lib then I am able to import. 如果将它们放在C:\\Python27\\Lib则可以导入。

I tried adding sys.path.append(os.path.abspath("/ MainFolder/<folder name>")) before importing but it is not working. 我尝试在导入之前添加sys.path.append(os.path.abspath("/ MainFolder/<folder name>")) ,但它不起作用。 How do I import them ? 如何导入它们?

Please try the solution: 请尝试解决方案:

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

from Importing files from different folder in Python Python中的其他文件夹导入文件

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

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