0

I have a python app that I have moved recently from the Windows box I developed it on to a Linux (CentOS) server. Everything runs fine in PyCharm on my workstation I wrote the code on, however, upon zipping/tarring the containing folder, regardless of where I move it, I get the following error when attempting to execute the application:

python ..\CONTROLLER\SFTPController.py
    Traceback (most recent call last):
    File "..\CONTROLLER\SFTPController.py", line 3, in <module>
    from SFTP import SFTPGet,SFTPPut
    ImportError: No module named SFTP

The above will occur if I move the copy of the source directory anywhere other than where it was developed originally.

I've done a little research on this and it doesn't matter what my current working directory is. The result is always what you see above. My Python experience has been limited to single file one-off scripts normally, so this is something I have no experience with previous.

If it helps, here is my application structure:

Top level directory: Filestuff

Filestuff
|... __init__.py
|... .idea (PyCharm created directory)
|... CONTROLLER|
               |... __init__.py
               |... SFTPController.py
|... SFTP|
         |... __init__.py
         |... SFTPGet.py
         |... SFTPPut.py
|... ZIP|
        |... __init__.py
        |... ZIPWork.py

As you can see, Filestuff is the top level. It contains init.py and the 3 other directories that contain their .py files as well as their init.py files. This to me looks like a standard Python package that should work if you import it to any system without changing the structure. I'm sure it's something stupid simple that I must have missed. I really appreciate any help in figuring this out!

  • What versions of Python are you using on the on the Windows machine and the CentOS machine? Import behavior changed with PEP 328 (https://www.python.org/dev/peps/pep-0328/) as of python 2.5. – A. L. Flanagan Mar 11 '15 at 21:45

2 Answers2

1

OK, I answered this twice and didn't like my answers, but I've thought of something that works. Instead of appending to the os.path, modify the load path before the import, and use a relative path:

import sys
sys.path.append("..")
from SFTP import SFTPGet, SFTPPut

(Note that os.path changes the system environment, but sys.path is where the interpreter looks for directories to search for imports). This avoids having to reorganize your file layout or hardcode a path.

In the future, you probably want to have your main script file somewhere outside the package hierarchy. If you put it in FileStuff/, python can find the import without the above trick.

Also see this answer regarding the challenges of executing a script within a package.

A. L. Flanagan
  • 1,162
  • 8
  • 22
  • Thank you! This works like a charm as well and seems to be a better way to go about getting my files to execute properly. I appreciate the thoroughness!!!! – Anthony Hopkins Mar 12 '15 at 21:21
0

Using the below line before my imports for the SFTP module fixed my issue:

os.path.append("/path/to/filestuff/")

I don't feel like this is the best method for this, so anyone who knows a better way I would more than appreciate the input, as this would need to be changed depending on where I am deploying the code....