当前位置: 动力学知识库 > 问答 > 编程问答 >

importerror - Python Import Error - specific case

问题描述:

I know there has been a ton of questions about this, but mine is pretty specific and i don't really know why the import is not working.

I've got the following Folder Structure:

importmodule

-classes

-pluginhelper

-- __init__.py

-plugins

-- plugin_a

-- plugin b

-- ..

-__init__.py

-<other py files>

In my plugin files i am importing the pluginhelper like this:

from importmodule.pluginhelper import function1, function2, ...

The functions are defined in the __ init __.py

Executing my plugin files works great on my windows machine. When i do it on the server, i get following Exception:

 ImportError: No module named 'importmodule'

The curious thing is, that i do get the error from every plugin, but one. In one of the plugins the import works, while there is no difference in the import statements.

I am using python 3.5 on both machines, while on the server my application is within a docker container.

Edit:

Setting the sys.path didnt work either :

 print("Indexed Path for Package.")

sys.path.index(os.getcwd())

I found the possible problem:

The folder structure in my dockercontainer seems to be different. I will fix it and tell you if it helped. Still i don't understand why it works with the one plugin using the same statement.

网友答案:

I want to share my solution with you, since anybody reading the question seems to have similar problems.

The problem occured because the package werent included in pythonpath / syspath. I am calling the plugins in a new thread with a script called execute.py. This is called via cmd from the main.py because i can't multiprocess my classes. And since the plugins are located in ../plugins, python only includes this path. Not the /importmodule path. Somehow it seems that it has been included on windows, i dont really know what has happened there tbh.

What i did was, i included the sys.path.append stuff on my execute.py which executes the plugins as follows: from pluginhelper import scrapeHostnames, hostnamesToIps, getSource. My IDE marks the import statement red now, but it is working, since i am appending the package.

分享给朋友:
您可能感兴趣的文章:
随机阅读: