+3 votes
3 views
in Python by (3.4k points)
edited by

How can I import the functions from file.py to another Python file which is in:

app/app1/Folder1/xyz.py

from this folder structure:

app/app1/folder1/xyz.py

I've also tried some other methods like one given below, How can I perform this task?

from app.app.folder1.xyz import func_name

2 Answers

0 votes
by (2k points)
edited by

Use sys.path function:

# some_file.py
import sys
sys.path.insert(0, '/path/to/app/app1/xyz')

import file

There are several other methods too but I find this one more easy and efficient. To know more click here.

0 votes
by (107k points)
edited by

Actually, you are doing right just make sure the folder that you want to import must contain an __init__.py, this allows it to be included as a package.

from application.app.folder.file import func_name

You can use the following video tutorials to clear all your doubts:-

Related questions

0 votes
2 answers
0 votes
1 answer
asked Jun 26, 2019 in Python by Anurag (33.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Oct 3, 2019 in Python by Tech4ever (20.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...