0

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!

2 Answers2

1

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

Community
  • 1
  • 1
MaLiN2223
  • 1,290
  • 3
  • 19
  • 40
  • I am not getting the following error after trying that: [root@vodgsnxf-po-a2p gsnworkflow]# python ./GSNEventLoop/EventLoop.py Traceback (most recent call last): File "./GSNEventLoop/EventLoop.py", line 6, in from ..Modules import FileOperations ValueError: Attempted relative import in non-package – Anthony Hopkins Jan 27 '17 at 17:03
  • I am not sure why this is being so difficult...Never had any trouble like this before. – Anthony Hopkins Jan 27 '17 at 17:37
  • @AnthonyHopkins Why are you a) root and b) attempting to call EventLoop.py directly instead of going through it's package? – xrisk Jan 27 '17 at 17:55
  • @Rishav I am root because the files I am working on are root-only accessible sensitive data, so they decided to lock files down to root access.....Guess they have never heard of a service account. The environment itself is sanitary and has no access to the internet and I am the only dev that has access to the server. Just makes sense in this scenario. If I FUBAR the box, then it's on me. As for your second question, I realized what I did wrong after looking over everything again but before I read your comment, which , ultimately, would have been my golden ticket had I seen it before. – Anthony Hopkins Jan 27 '17 at 18:21
0

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.