0

I'm trying to import modules from separate paths but the error it's returning is "module not found." It's importing modules from the directory the script is executed in but will not change directories and import from said directory.

print(os.getcwd())

When I run this, before it throws the error it couldn't find the module, it will output the parent directory so for example I'll go with test\import\modules.

I'll run a script in \import\ to import test_0.pyd from \import\ and test_1.pyd from \modules (test.py and test_0 being located in \import\ and test_1 being located in \modules. Also, I have tried relative importing and every directory contains init.py).

import test_0 # this would work
from modules import test_1 # throws error that module isn't found

So I run the print command and it returns that it's trying to import from test\ and I've tried changing directories but it'll say the working directory changed when I print, but still outputs that it couldn't find the module. Any help is greatly appreciated, thank you.

EDIT http://prntscr.com/6ch7fq - executing test.py http://prntscr.com/6ch80q - import directory

Norghar
  • 3
  • 3
  • 1
    You can add paths with modules as follows `sys.path.append('../import')` for example. – Marcin Mar 04 '15 at 05:08
  • I've tried that too but it still throws the "module not found" error. – Norghar Mar 04 '15 at 05:09
  • so please, clarify your project structure. Its not clear from the question where is what, and how you call your program. – Marcin Mar 04 '15 at 05:10
  • main.py is supposed to import both files but won't change directories to modules which has the test_1.pyd file to import it. Sorry if this is confusing, I really do not know how else to put this – Norghar Mar 04 '15 at 05:14
  • You can also use relative imports, e.g. `import ..forder1.foder2.mymodule` which translates to `../folder1/folder2/mymodule.py` – Marcin Mar 04 '15 at 05:16
  • I've also tried that but it still returns that it couldn't get the module. Would a prntscr screenshot help? – Norghar Mar 04 '15 at 05:17
  • Any additional info/screenshot would be useful. The best would be minimal working example, that we can actually recreate. Otherwise, as you see, this is more of a guessing game. – Marcin Mar 04 '15 at 05:20
  • I've edited with screenshots, I tried to fit one more in but http://prntscr.com/6ch84t it's just the folder that contains test_1 pyd – Norghar Mar 04 '15 at 05:34
  • Please show your `test.py` and whats inside modules folder. – Marcin Mar 04 '15 at 05:34
  • for test.py `from test_0 import ExecFTPDL from modules import test_1` and for the modules folder the screenshot in my last comment – Norghar Mar 04 '15 at 05:38

2 Answers2

2

When you start python from a directory, that directory is added to your PYTHONPATH so modules are importable from that directory and below, provided you've got an __init__.py in each directory, including the top level that you're running python from. See here:

~/Development/imports $ tree . ├── __init__.py ├── mod1 │   ├── __init__.py │   ├── a.py ├── mod2 │   ├── __init__.py │   ├── b.py ├── top.py

So when we start python from ~/Development/imports/, we can access top mod1.a and mod2.b:

~/Development/imports $ python
Python 2.7.8 (default, Nov  3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import top
>>> import mod1.a
>>> import mod2.b
>>> import sys

But when we start python from inside mod1, we're not allowed to go outside of mod1 back to top or to mod2:

~/Development/imports/mod1 $ python
Python 2.7.8 (default, Nov  3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> from .. import top
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
>>> from ..mod2 import b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package

Relative imports from ..mod2 import b only work from a module below the top level module that you started from, because they are all implicitly in the python path.

You can't escape outside of the module you start from unless that particular path is added to PYTHONPATH or sys.path:

~/Development/imports/mod1 $ PYTHONPATH=../ python
Python 2.7.8 (default, Nov  3 2014, 11:21:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> import top
>>> import top.mod2.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mod2.b
>>> import sys
>>> sys.path.append('~/Development/imports/mod2/')
>>> from mod2 import b
>>>

So, you need to ensure that all of your directories have an __init__.py file in them. You also need to ensure that you're starting python from the right location, usually the top directory. You can't start python half way down the directory structure and expect to get back up to the top, or sideways to another directory/module.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • that's what it was wow. I moved it up to the root changed the import lines and it worked simple fix for such a buggy problem haha thanks for this you explained it well I'll accept as soon as I can! – Norghar Mar 04 '15 at 05:52
0

Do you have __init__.py file in the said modules/directory? This is required for python to treat it as a package.

Check out What is __init__.py for?

Community
  • 1
  • 1
viczsaurav
  • 37
  • 6