简体   繁体   中英

importing file from another directory

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?

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.

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

from PycharmProjects.pythonProject1 import test1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM