简体   繁体   English

Python:如何将与__main__相同目录中的模块导入第三方模块?

[英]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.

相关问题 如何将模块导入为 __main__? - How to import a module as __main__? 如何在.NET中导入第三方IronPython模块? - How do I import a third-party IronPython module in .NET? 如何在 Odoo 中导入在第三方模块中声明的 Python 纯类? - How to import a Python pure class declared in a third-party module in Odoo? 如何在Python中将模块导入一个既用作__main__又被另一个目录中的文件导入的文件? - How to import a module in Python in a file that is both used as a __main__ and is imported by a file in a different directory? 如何从 python 中的 __main__ 导入相关模块 - How to import relative module from __main__ in python 如何在不编辑原始模块的情况下从第三方Python模块向类添加方法 - How do I add a method to a class from a third-party Python module without editing the original module Python 3 - 在同一目录中导入.py文件 - ModuleNotFoundError:没有名为'__main __。char'的模块; '__main__'不是包 - Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package 如何在Python中获取__main__模块的文件名? - How to get filename of the __main__ module in Python? 从第三方Python模块中提取信息? - Extracting information from a third-party Python module? 使用阻止Flask应用程序的第三方Python模块 - Using third-party Python module that blocks Flask application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM