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.
Import files directly from file location - Ideas - Discussions on Python.org
Importing python files from different folder
Import Class from another python file?
At the top of your script, add the following:
import sys print(sys.path)
This will help you understand why certain imports work. sys.path is a list of directories. When you say import foo, Python will look for something called "foo" in each of the directories in sys.path. The first directory in sys.path is typically the directory that contains the script you are executing (but this can change depending on exactly how you are executing the script).
If you can show the full structure of your project and exactly how you are running your script, then I can give you advice on the best way to do your imports.
More on reddit.com[Python][Pandas] Reading a .csv file from a folder on my hard drive and setting it to a variable
This is because \r is the carriage return character. When that's found in a string, it moves the "reader" back to the beginning of the line. Your string, therefore, is read as 'adiation_data.csv', which doesn't exist. You can precede your string with r to have metacharacters not be interpolated, as in:
dataset_all=read_csv(r'C:\Users\Jason\Desktop\open_datasets\radiation_data.csv')More on reddit.com
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.