Home > front end >  Import from python file located in sub folder
Import from python file located in sub folder

Time:02-02

I'm using vs code. I'm running file1.py, it imports a function from file2.py.

The file structure is as follows:

feeds
├── bulk_load
│   ├── __init__.py (empty)
│   └── file2.py
├── __init__.py (empty)
└── file1.py

in file1.py the following works:

from bulk_load.file2 import func123

but the following doesn't:

sys.path.append("bulk_load")
from file2 import func123

Error is

ModuleNotFoundError
No module named file2

I dont really understand why.

CodePudding user response:

I have just recreated with the file structure

feeds
    file1.py
    bulk_load
        file2.py

in file1.py:

import sys
sys.path.append("bulk_load")

from file2 import func123
func123()

in file2.py

def func123():
    print('hello')

and running python file1.py from feeds outputs:

hello

so I am unable to recreate your error, sys.path.append works fine.

Can you print sys.path and see if that looks correct?

CodePudding user response:

You're importing it the wrong way. There's no need to use something like sys.path.append(). bulk_load dir is automatically considered as a module (Python 3.3 ), so you should import right from it.

./bulk_load/file2.py

def print_test():
    print("Here we go")

./file1.py

from bulk_load.file2 import print_test

print_test()

Run:

$ python ./file1.py
Here we go

CodePudding user response:

It is how entries in [Python.Docs]: sys.path are handled (absolute vs. relative).
I've searched [Python.Docs]: Modules - The Module Search Path (and a couple of other pages) but I didn't find a way that clearly states it.

I prepared the following structure (I'll be reusing this console):

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> tree /a /f
Folder PATH listing for volume SSD0-WORK
Volume serial number is AE9E-72AC
E:.
|   code00.py
|
 ---mod_dir
|       mod00.py
|
\---test_dir
  • code00.py:

    #!/usr/bin/env python
    
    print(__file__)
    
    import os
    import sys
    
    MOD_DIR = "mod_dir"
    
    if len(sys.argv) > 1 and sys.argv[1] == "full_path":
        print("Full path")
        sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), MOD_DIR))
    else:
        print("Just dir name")
        sys.path.append(MOD_DIR)
    
    print("CWD:", os.getcwd())
    
    from mod00 import dummy
    
  • mod00.py:

    print(__file__)
    
    dummy = 1.618
    

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" ./code00.py full_path
e:\Work\Dev\StackOverflow\q075313871\code00.py
Full path
CWD: e:\Work\Dev\StackOverflow\q075313871
e:\Work\Dev\StackOverflow\q075313871\mod_dir\mod00.py

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]>
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" ./code00.py
e:\Work\Dev\StackOverflow\q075313871\code00.py
Just dir name
CWD: e:\Work\Dev\StackOverflow\q075313871
e:\Work\Dev\StackOverflow\q075313871\mod_dir\mod00.py

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]>
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871]> cd test_dir

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871\test_dir]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" ../code00.py full_path
e:\Work\Dev\StackOverflow\q075313871\code00.py
Full path
CWD: e:\Work\Dev\StackOverflow\q075313871\test_dir
e:\Work\Dev\StackOverflow\q075313871\mod_dir\mod00.py

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871\test_dir]>
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q075313871\test_dir]> "e:\Work\Dev\VEnvs\py_pc064_03.10_test0\Scripts\python.exe" ../code00.py
e:\Work\Dev\StackOverflow\q075313871\code00.py
Just dir name
CWD: e:\Work\Dev\StackOverflow\q075313871\test_dir
Traceback (most recent call last):
  File "e:\Work\Dev\StackOverflow\q075313871\code00.py", line 19, in <module>
    from mod00 import dummy
ModuleNotFoundError: No module named 'mod00'

As seen, relative paths depend on the current location (CWD), so in order to make sure that your code works from every location, append the full path.
Of course there are alternatives, but I'm not going to insist on them.

For more details on this kind of errors (and ways to get past them) check:

  • Related