简体   繁体   中英

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. I want to config.py have been imported in shared_lib before.

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.

/project
       *Main.py
       *Config.py
       * __init__.py
       /test
            *hello.py
            __init__.py

lets say i want to import hello.py and 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.

If this dosent work, then add the path to environment variables pythonpath

In shared_lib

import project1.config as project1
import project2.config as project2
import project3.config as project3

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