1

I have a structure like this

pkg1
   __init__.py  
   file_1.py
   file2.py
pkg2
   test_1.py
   __init__.py
 

I have a structure like above. I have a code in test_1.py which is trying to load file_1.py

#test_1.py

from pkg1 import file_1
# from .. import file_1

Both statements are not working. How can I import the file_1 from pfg1

Sumit
  • 1,953
  • 6
  • 32
  • 58

1 Answers1

0

A python program can modify its path by adding elements to sys.path directly. import sys then you can add to the system-path at runtime:

    import sys
    sys.path.insert(0, 'path/to/your/py_file')
    
    import py_file

or you can do this:

    import sys
    import os
    
    base_dir = os.path.dirname(__file__) or '.'

    # Insert the package_dir_a directory at the front of the path.
    package_dir_a = os.path.join(base_dir, 'package_dir_a')
    sys.path.insert(0, package_dir_a)
    import py_file
MIARD
  • 126
  • 1
  • 7