简体   繁体   中英

Import modules from different folder (python)

I have a folder, which contains two separate folders, one of which holds some python modules, and the other one holds a python script that uses those modules:

parentFolder/
    lib/
       __init__.py
       readFile.py
       writeFile.py
    folder/
       run.py

The __init__.py file is empty. In run.py , I have the following:

from ..lib import readFile

data = readFile('file.dat')

This gives me the error

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from ..lib import readFile
ValueError: Attempted relative import in non-package 

What am I missing?

You need to add __init__.py files (can be empty) to each of the directories to make them a package. See the documentation for more details.

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