1

Here is the Project structure -

MAIN Project FOLDER:

  • file1.py
  • Directory1
    • testFile.py

file1.py is in the Main Project folder.

testFile.py is under Directory1.

I need to import file1.py into testFile.py.

If I just add import file1 in testFile.py, it gives me error - ModuleNotFoundError: No module named 'file1'

  • There is a custom exception. I'm trying to import ``` from file1 import excName``` –  Nov 15 '19 at 01:39
  • Try importing with with complete path https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path – Swarnveer Nov 15 '19 at 01:41

1 Answers1

0

You need to add the path to the sys.path in testFile.py, can you try this:

import sys
sys.path.append(path.join(path.dirname(__file__), '../'))
import file1

Hope this helps.

Bill Chen
  • 1,699
  • 14
  • 24
  • Where exactly should I add this piece of code? In testFile.py? In testFile.py, I get the error on line ``` import file.py```. Should I write these 2 lines before ``` import file.py```? –  Nov 15 '19 at 23:11