skip to Main Content

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.

3

Answers


  1. 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?

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
  3. 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:WorkDevStackOverflowq075313871]> 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:WorkDevStackOverflowq075313871]> "e:WorkDevVEnvspy_pc064_03.10_test0Scriptspython.exe" ./code00.py full_path
    e:WorkDevStackOverflowq075313871code00.py
    Full path
    CWD: e:WorkDevStackOverflowq075313871
    e:WorkDevStackOverflowq075313871mod_dirmod00.py
    
    [cfati@CFATI-5510-0:e:WorkDevStackOverflowq075313871]>
    [cfati@CFATI-5510-0:e:WorkDevStackOverflowq075313871]> "e:WorkDevVEnvspy_pc064_03.10_test0Scriptspython.exe" ./code00.py
    e:WorkDevStackOverflowq075313871code00.py
    Just dir name
    CWD: e:WorkDevStackOverflowq075313871
    e:WorkDevStackOverflowq075313871mod_dirmod00.py
    
    [cfati@CFATI-5510-0:e:WorkDevStackOverflowq075313871]>
    [cfati@CFATI-5510-0:e:WorkDevStackOverflowq075313871]> cd test_dir
    
    [cfati@CFATI-5510-0:e:WorkDevStackOverflowq075313871test_dir]> "e:WorkDevVEnvspy_pc064_03.10_test0Scriptspython.exe" ../code00.py full_path
    e:WorkDevStackOverflowq075313871code00.py
    Full path
    CWD: e:WorkDevStackOverflowq075313871test_dir
    e:WorkDevStackOverflowq075313871mod_dirmod00.py
    
    [cfati@CFATI-5510-0:e:WorkDevStackOverflowq075313871test_dir]>
    [cfati@CFATI-5510-0:e:WorkDevStackOverflowq075313871test_dir]> "e:WorkDevVEnvspy_pc064_03.10_test0Scriptspython.exe" ../code00.py
    e:WorkDevStackOverflowq075313871code00.py
    Just dir name
    CWD: e:WorkDevStackOverflowq075313871test_dir
    Traceback (most recent call last):
      File "e:WorkDevStackOverflowq075313871code00.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:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search