[英]Python: How to import module that is in the same directory as __main__ to third-party module?
I got next structure of package: 我得到了包的下一个结构:
/
__init__.py
shared_lib.py
project1/
__init__.py
main.py
config.py
project2/
__init__.py
main.py
config.py
project3/
__init__.py
main.py
config.py
...
What I do is start main.py for every project directory.There shared_lib
being imported. 我要做的就是开始main.py每个项目directory.There shared_lib
被导入。 I want to config.py
have been imported in shared_lib
before. 我想config.py
之前已经导入了shared_lib
。
How could it be done? 怎么做? So i need something like 所以我需要像
import os.path.dirname(os.path.abspath(__main__.__file__)).config
I dont understand, what you mean. 我不明白,你的意思。 But 但
If you have 2 folders, with 2 files each you can import them like this. 如果您有2个文件夹,每个文件夹有2个文件,则可以这样导入它们。
/project
*Main.py
*Config.py
* __init__.py
/test
*hello.py
__init__.py
lets say i want to import hello.py and Config.py 可以说我要导入hello.py和Config.py
import Config as conf
import test.hello as hel
in your config file. 在您的配置文件中。
import shared_lib as shared
I am using eclipe as workspace editor. 我正在使用eclipe作为工作区编辑器。
If this dosent work, then add the path to environment variables pythonpath 如果此方法有效,则将路径添加到环境变量pythonpath中
In shared_lib 在shared_lib中
import project1.config as project1
import project2.config as project2
import project3.config as project3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.