简体   繁体   English

无法从包中导入python模块

[英]Can not import python modules from package

My project path is like: 我的项目路径如下:

main.py
modules/
     __init__.py
     databaseManager.py
     sync.py
     excel.py

in main.py: 在main.py中:

from modules.databaseManger import addExcelToDb, searchInDbAll
from modules.excel import search, showExcelDirContents
from modules.sync import syncExcelAndDB

and for example in database.py : 例如在database.py中:

from modules.excel import showExcelDirContents
from modules.sync import insertExcelNameToSyncDb

but when I run main.py I get this error: 但是当我运行main.py时,出现此错误:

Traceback (most recent call last):
 File "main.py", line 6, in <module>
   from modules.databaseManger import searchIn
ImportError: cannot import name 'searchInDbAll'

and also having error when trying to import a function from each file in modules directory to others. 尝试将功能从modules目录中的每个文件导入到其他文件时也出错。

I need some examples of import ing. 我需要一些import示例。

You can append to your path where you put your modules like this: 您可以将路径附加到放置模块的位置,如下所示:

import sys
sys.path.append('modules/')

or 要么

import sys
sys.path.append('c:/mycode/stuff/modules/')

note those are forward slashes, or you can use double backslashes like \\\\ 请注意,这些是正斜杠,或者您可以使用双反斜杠,例如\\\\

Then just have your databaseManger.py file in /modules 然后只需将您的databaseManger.py文件放在/ modules中

You'll also need to have a file in the /modules folder named: 您还需要在/ modules文件夹中有一个名为:

__init__.py

Which is just an empty file 这只是一个空文件

Then you should be able to do: 然后,您应该可以执行以下操作:

from databaseManger import addExcelToDb, searchInDbAll

This is circular import issue. 这是循环导入问题。

Explanation: 说明:

You start by triggering import of databaseManager module. 首先,触发databaseManager模块的导入。

During this databaseManager code starts to import excel . 在此期间, databaseManager代码开始导入excel

During excel importing, excel code tries to retrieve function searchInDbAll() from databaseManager . excel导入期间, excel代码尝试从databaseManager检索函数searchInDbAll() But at that moment this function does not exist - because databaseManager is in process of importing excel and he hasn't started defining any functions. 但是到那时,此功能还不存在-因为databaseManager正在导入excel并且他还没有开始定义任何功能。

How to fix: 怎么修:

In modules where circular import conflicts exist, import modules instead of functions. 在存在循环导入冲突的模块中,请导入模块而不是功能。 For example, change this: 例如,更改此:

from modules.excel import showExcelDirContents

to that: 对此:

from modules import excel

And of course, you must then change corresponding function calls, from showExcelDirContents() to excel.showExcelDirContents() . 当然,然后您必须将相应的函数调用从showExcelDirContents()更改为excel.showExcelDirContents()

You must do this in your databaseManger , excel and sync modules. 您必须在databaseMangerexcelsync模块中执行此操作。 With this fix, I actually could run your code. 有了此修复程序,我实际上可以运行您的代码。

And yeah, remove appends to sys.path, that is wrong 是的,删除sys.path的追加,那是错误的

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

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