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 from the __init__.py in the same directory? - Stack Overflow
if I use __init__.py to make a directory of modules importable, can I still do 'from <directory> import <module> ?
importing a module from a different folder
what can I put in __init__.py to make them useful
I see 'import from parent module' as an anti-pattern in Python. Imports should be the other way around. Importing from modules's __init__.py is especially problematic. As you noticed, importing module foo.bar from foo/bar.py involves importing foo/__init__.py first, and you may end up with a circular dependency. Adding a print("Importing", __name__) to your init files helps see the sequence and understand the problem.
I'd suggest that you moved the code you want to import in conditions.py from __init__.py to a separate lower-level module, and just import some names from that module in __init__.py to expose it at higher level.
Let's suppose that you had some class Bar in your __init__.py. I'd reorganize it the following way.
__init__.py:
from bar import Bar # exposed at the higher level, as it used to be.
bar.py:
class Bar(object): ...
conditions.py:
from . import Bar # Now it works.
Ideally an __init__.py should contain nothing but imports from lower-level modules, or nothing at all.
If you get
ImportError: attempted relative import with no known parent package
when you do like
import . from something
especially from the script executed, just try
from __init__ import something
Even though it could be problematic when there are many __init__.pys in sys.path, it would be helpful in some simple situaions.
so If I have a directory called foo with a bunch of modules in it (bar.py, baz.py) and an empty __init__.py in teh dir, can I still do from foo import baz from my main .py script?
I want to organize all my modules in a dir, but I don't want to always import them all, or a fixed set of them (which seems to be the purpose of __init__.py). I usually only want to import some of them depending on the functionality of the main script.
So my intent is less to create an consistently importable package from a dir, but rather just organize my project so I don't have a bunch of .py modules in the root of my project dir.
so this is the directory tree, and i want to import the modules from main1.py inside main2.py
project |____folder1 | |____main1.py |____folder2 |____subfolder1 |_____subfolder2 |____main2.py
any help is very much appreciated