Anthony Hopkins
Anthony Hopkins

Reputation: 75

Getting Python import error for a package within my project

I am hoping someone could shine some light on a really odd situation I am running into today. I have a project I completed that runs great in PyCharm; however, when trying to call the project from the command line, this is the error I am receiving:

[root@vodgsnxf-po-a2p ~]# python /opt/gsnworkflow/GSNEventLoop/EventLoop.py
Traceback (most recent call last):
  File "/opt/gsnworkflow/GSNEventLoop/EventLoop.py", line 6, in <module>
   from Modules import FileOperations
ImportError: No module named Modules

Here is my file layout:

/opt/gsnworkflow/
|-- __init__.py  
|-- GSNEventLoop/
|   |-- __init__.py
|   `-- EventLoop.py
`-- Modules/
    |-- __init__.py
    |-- Configuration.py
    |-- Logging.py
    |-- FileOperations.py
    `-- Database.py

I have tried a bunch of different sys.path.append commands, such as the following:

sys.path.append('/opt/gsnworkflow/')  
sys.path.append('/opt/gsnworkflow/Modules/')  
sys.path.append('/opt/gsnworkflow/GSNEventLoop/')  

None of these options have resolved my issue at all and I am getting to my wit's end. Does anyone see anything glaringly obvious that I may have missed or done incorrectly? I truly appreciate anyone who can figure this out. Thanks!

Upvotes: 0

Views: 149

Answers (2)

Anthony Hopkins
Anthony Hopkins

Reputation: 75

I figured this out. My main function existed within the GSNEventLoop directory. I moved it out and to the root of the Python package and voila. All imports work as expected. So now, My structure looks like this:

  • /opt/gsnworkflow/
    • __init__.py
    • /GSNEventLoop/
      • __init__.py
      • EventLoop.py
    • /Modules/
      • __init__.py
      • Configuration.py
      • Logging.py
      • FileOperations.py
      • Database.py
    • GSNMain.py <-- This guy contains code that executed that calls the event loop.

Once I established the GSNMain.py file, all imports and functionality works as expected. Thanks to those who reached out early and quickly to try to provide support for this. I love StackOverflow.

Upvotes: 0

MaLiN2223
MaLiN2223

Reputation: 1299

What you are trying to do is call module Modules from GSNEventLoop. You need to go module up and then call Modules.

This is how it is done in python 3:

from ..Modules import FilesOperations

Further reading and look for Intra-package References

About how is it done in python 2 see THIS

Upvotes: 1

Related Questions