Solving problem is about exposing yourself to as many situations as possible like Importing files from different folder and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Importing files from different folder, which can be followed any time. Take easy to follow this discuss.
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?
Answer #1:
Note: This answer was intended for a very specific question. For most programmers coming here from a search engine, this is not the answer you are looking for. Typically you would structure your files into packages (see other answers) instead of modifying the search path.
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
Answer #2:
Nothing wrong with:
from application.app.folder.file import func_name
Just make sure folder
also contains an __init__.py
, this allows it to be included as a package. Not sure why the other answers talk about PYTHONPATH
.
Answer #3:
When modules are in parallel locations, as in the question:
application/app2/some_folder/some_file.py
application/app2/another_folder/another_file.py
This shorthand makes one module visible to the other:
import sys
sys.path.append('../')
Answer #4:
First import sys in name-file.py
import sys
Second append the folder path in name-file.py
sys.path.insert(0, '/the/folder/path/name-package/')
Third Make a blank file called __ init __.py in your subdirectory (this tells Python it is a package)
- name-file.py
- name-package
- __ init __.py
- name-module.py
Fourth import the module inside the folder in name-file.py
from name-package import name-module
Answer #5:
I think an ad-hoc way would be to use the environment variable PYTHONPATH
as described in the documentation: Python2, Python3
# Linux & OSX
export PYTHONPATH=$HOME/dirWithScripts/:$PYTHONPATH
# Windows
set PYTHONPATH=C:pathtodirWithScripts;%PYTHONPATH%
Answer #6:
Your problem is that Python is looking in the Python directory for this file and not finding it. You must specify that you are talking about the directory that you are in and not the Python one.
To do this you change this:
from application.app.folder.file import func_name
to this:
from .application.app.folder.file import func_name
By adding the dot you are saying look in this folder for the application folder instead of looking in the Python directory.
Answer #7:
The answers here are lacking in clarity, this is tested on Python 3.6
With this folder structure:
main.py
|
---- myfolder/myfile.py
Where myfile.py
has the content:
def myfunc():
print('hello')
The import statement in main.py
is:
from myfolder.myfile import myfunc
myfunc()
and this will print hello.
Answer #8:
From what I know, add an __init__.py
file directly in the folder of the functions you want to import will do the job.