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.
Hello. I have a code divided over different files like this:
-run_file
folder1
- function_file
folder2
-class_file
I can easily import functions defined in the function_file into the run_file. However I cannot import functions into the class_file because I somehow need to write a code so that the class_file can "look out" of folder2 and look in to folder1. I looked into the code of a person who had a similar project and there were these 'things':
sys.path.append("..")
dirname = os.path.dirname(__file__)He told me I could do it like this but it did not work for me. Could somebody give me a few hints on how I could achieve this?
python - import function from another folder's file - Stack Overflow
Accessing function from .py file in another directory
importing a module from a different folder
Importing python files from different folder
What you need to do is initialize the new directory as a package. In order to do this, inside the new directory make an empty file titled __init__.py. After you do this, go back to your main.py code and import it like this instead:
from new.test import (function)
Your new tree should look like this:
project/
ββ new/
β ββ test.py
| ββ __init__.py
ββ docs.py
ββ main.py
P.S.
If you are trying to import a function from docs.py into test.py, you probably should not do this. This is because it will result in an error known as a circular import. This will cause your script to no longer work. If you want to import a function from docs.py into test.py then put them in the same directory (or directory at the same level of the project hierarchy).
The issue is in how you are running your code - you should add the init files as in
project/
ββ new/
β ββ test.py
ββ __init__.py
ββ docs.py
ββ main.py
then run your code as
python -m new.test # note no py
from the project folder.
Hi,
I have trouble accessing a function that I created in one python file in a directory, by another python file in another directory.
The structure of the files as seen in the screenshot is:
Main Folder:
Folder 1:
file1.py (the function that I want to take is here)
Folder 2:
file2.py (I want to use the function from file1.py)
I saw online a method using sys, this doesn't seem to work. And also the method that you see in the screenshot using the dots - folder1.file1.
How do I access this function? (the intellisence suggests the folder, the file and the function, but one I use it in the code you can see the error in the terminal)
Image:
https://imgur.com/a/SC6W9OR
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
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.