Importing files from different folder in python

0 votes

I have the following folder structure.

application
├── app
│   └── folder
│       └── file.py
└── app2
    └── some_folder
        └── some_file.py

I want to import some functions from file.py in some_file.py.

I've tried

from application.app.folder.file import func_name

and some other various attempts but so far I couldn't manage to import properly. How can I do this?

Dec 19, 2020 in Python by anonymous
• 65,870 points
301 views

1 answer to this question.

0 votes

By default, you can't. When importing a file, Python only searches the directory that the entry-point script is running from and sys.path which includes locations such as the package installation directory (it's actually a little more complex than this, but this covers most cases).

However, you can add to the Python path at runtime:

# some_file.py
import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')

import file
answered Dec 19, 2020 by Nikita

Related Questions In Python

0 votes
0 answers

How to read lines from compressed text files in Python

Is it easy to read a line ...READ MORE

Dec 27, 2018 in Python by Anirudh
• 2,080 points
234 views
0 votes
1 answer

How to access different Anaconda environments from PyCharm for coding in Python?

Hi! In PyCharm, you can add any number ...READ MORE

answered Feb 11, 2019 in Python by Nymeria
• 3,520 points
583 views
0 votes
1 answer
0 votes
1 answer

How to resolve the error while importing cv2 from openCV in python?

I would recommend you go with anaconda ...READ MORE

answered Jul 31, 2019 in Python by Mohammad
• 3,210 points
601 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,858 views
0 votes
0 answers
0 votes
1 answer

Delete column from pandas DataFrame in python

As you've guessed, the right syntax is del ...READ MORE

answered Dec 19, 2020 in Python by Gitika
• 65,870 points
45 views
0 votes
1 answer