Home > Software engineering >  importing file from another directory
importing file from another directory

Time:12-04

There are multiple questions like this but and i tried every method from them i can't figure out why it is not working i have file directory in "C" drive like this

PycharmProjects

          ├── pythonProject1
          │   └── test1.py
          └── pythonProject2
              └── test2.py

Now i want to import test1.py from pythonproject1 to pythonproject2 test2.py

import sys
sys.path.append('C:\\Users\\jimmy\\PycharmProjects\\pythonProject1')
import test1

This code gives me error "No module named 'test1'" i am using python 3.6 and have no "init" file in both folders, and if "init" file is needed how can i add that?

CodePudding user response:

In your root directory add init.py

root
 ├── pythonProject1
          └── test1.py
          └── __init__.py
 └── pythonProject2
          └── test2.py
          └── __init__.py
 └── __init__.py

Once you do this directories will be treated as modules. __ init __ is just empty python file.

CodePudding user response:

this could work if "PycharmProjects" is the main Pycharm directory:

from PycharmProjects.pythonProject1 import test1
  • Related