I want to know how to import files in different folders

Asked 2 years ago, Updated 2 years ago, 135 views

at application/app/folder1/file.py

How do I import application/app2/folder2/import_file.py?

from application.app.folder2.import_file import func_name I thought this would work, but it's not working ㅜ<

I think there's a way, but I'm not sure. Help me

python python-import import-에러

2022-09-22 12:51

1 Answers

The Python default setting allows you to import only files in the same directory as the script to run, or files in sys.path

You must set path to import files in different directories.

To add a path to sys.path, write like this in the Python script

# file.py
import sys
sys.path.insert(0, '/path/to/application/app/folder2')

import import_file


2022-09-22 12:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.