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
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
Answer from Cameron on Stack OverflowNote: 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
# caution: path[0] is reserved for script path (or '' in REPL)
sys.path.insert(1, '/path/to/application/app/folder')
import file
There is 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. I am not sure why the other answers talk about PYTHONPATH.
python - How to import other scripts in the same folder? - Geographic Information Systems Stack Exchange
How to import python file from folder as folder.file using "from" keyword?
Import files directly from file location - Ideas - Discussions on Python.org
How to import python files present in a folder(within a container) in another python file?
Sorry, I've read multiple answers, but just can't get this to work...
Let's say I have this project structure:
--project
--files_1
--file_1.py
--files_2
--file_2.pyHow can I import classes from file_2.py into file_1.py?Thank you for the help.
You can prepend the directory where interfaceParam.py is saved to your PATH and then import the script.
In the example the interfaceParam script is imported under the namespace ip.
import sys
sys.path.insert(0, r"C:/directory/where/script/is/saved")
import interfaceParam as ip
ip.myFunction()
Or, import individual functions
import sys
sys.path.insert(0, r"C:/directory/where/script/is/saved")
from interfaceParam import myFunction
myFunction()
you can try:
from . import interfaceParam, bufferBuilder, clipping, graphicBuilder, endDialog
remove ".py" and using point as current directory.
Also:
from {your plugin name} import interfaceParam, bufferBuilder, clipping, graphicBuilder, endDialog
But best way is import not script, but functions or classes from file:
from .interfaceParam import class1, function1, class2