简体   繁体   中英

How do I import module?

The directory structure is as follows:

folder1
  __init__.py
  file1.py
  folder2
    file2.py

If I write file2.py as follows:

from folder1 import file1

I get the error No module named 'folder1' .

If I write file2.py as follows:

from ..folder1 import file1

I get the error ValueError: attempted relative import beyond top-level package .

How can I import file1 from file2?

Try the following:

import sys
sys.path.append("..")
from .. import file1

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