0

I know how to import a package or module, but I meet a quite strange problem.

If I run swmm5_extend_function/example.py, everything is fine. However, when I run example.py, errors occur:

Traceback (most recent call last):   
File "example.py", line 2, in <module>
    from swmm5_extend_function.Swmm5Extend import SWMM5ReadInp   
File "C:\project\swmm5_extend_function\Swmm5Extend.py", line 1, in <module>
    import swig.SWMM5ReadInpFile as swmm 
ModuleNotFoundError: No module named 'swig'

Here is my project structure:

project/     
-- example.py   
-- ......   
-- swmm5_extend_function/  
      -- __init__.py
      -- example.py
      -- Swmm5Extend.py
      -- swig/
           -- __init__.py
           -- SWMM5ReadInpFile.py
           -- ....

Here is code of each .py file:

swmm5_extend_function/Swmm5Extend.py

import swig.SWMM5ReadInpFile as swmm

class SWMM5ReadInp(object):
    pass

swmm5_extend_function/example.py

from Swmm5Extend import SWMM5ReadInp

example.py

from swmm5_extend_function.Swmm5Extend import SWMM5ReadInp

I want to know why this strange error happens.

  • 1
    `swig/` is in the same folder as `swmm5_extend_function/example.py`, but not `example.py`. – MisterMiyagi Feb 11 '19 at 14:13
  • 2
    Change `import swig.SWMM5ReadInpFile as swmm` to `import swmm5_extend_function.swig.SWMM5ReadInpFile as swmm` – zamir Feb 11 '19 at 14:14
  • 1
    Possible duplicate of [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – MisterMiyagi Feb 11 '19 at 14:16
  • you're probably running the command from the `/project/` folder. If that's the case, you're calling 2 different `example.py` files. – Gsk Feb 11 '19 at 14:39
  • @Gsk Yes, so my question is why the error occurs only when I am calling example.py. Both "example.py" and "swmm5_extend_function/example.py" can reach Swmm5Extend.py, but only example.py will report a error. – RUOZHOU LIN Feb 12 '19 at 01:30
  • @MisterMiyagi Thanks for you relpy! I know what you mean, but how can I improve my code to solve this problem? I think the link below cannot solve my problem. – RUOZHOU LIN Feb 12 '19 at 01:32

1 Answers1

0

For a better explanation, I've created the following folder structure:

test/     
-- __init__.py
-- greeter.py # Greets in German
-- subfolder/  
    -- __init__.py
    -- greeter.py # Greets in Italian
    -- test.py
    -- deepfolder/
        -- __init__.py
        -- greeter.py # Greets in France

As you may notice, we have 3 files with the same name, each one greets in a different language using a function with the same name. The only function in a greeter.py file is:

def says():
    print("Hello World!")

IMPORT FROM THE SAME FOLDER
If from test.py file we import greeter and run the says function, we'll have:

import greeter as greeter

greeter.says()

Output:

Buongiorno Mondo! # Italian output

IMPORT FROM A SUBFOLDER
But what if we want to import from a subfolder?
To import from a subfolder (i.e., deepfolder/), we simply add an empty __init__.py file into the folder, then we can specify the path in the import:

import deepfolder.greeter as greeter

greeter.says()

Output:

Bonjour le Monde! # France output

IMPORT FROM A PARENT FOLDER
At last, you may want to import from a parent folder.
You should try to have your main running file at the top of the folder tree, but things happens and you find yourself looking to import a module from a parent folder.
For doing this, you need to add the parent folder to the sys.path:

import sys
sys.path.append("/path/to/dir")
from test import greeter as greeter

greeter.says()

Output:

Guten Morgen Welt! # German output

Importing scripts and modules isn't really the most pythonic way to solve things, you may want to have a look on Python's documentation about packages.

TL;DR
In your project/example.py use

import swmm5_extend_function.swig.SWMM5ReadInpFile as swmm

instead of

import swig.SWMM5ReadInpFile as swmm
Gsk
  • 2,929
  • 5
  • 22
  • 29