5

I know there are similar questions asked but what I found is not very clear to me in this simple setting. Given this directory tree how can I import a function from file1.py into file2 (we call the interpreter from the file2.py)? I would like this setting to work independently on where main_folder is, that is if I copy main_folder to a different directory, the imports would still work well.

main_folder
 folder1
 file1.py (with a function func())
 folder2
 file2.py
alex_noname
33.2k6 gold badges95 silver badges110 bronze badges
asked Nov 25, 2020 at 11:33
4
  • What/where is the start script with which the Python interpreter is called initially? Commented Nov 25, 2020 at 11:37
  • The starting script is the file2.py Commented Nov 25, 2020 at 11:39
  • 2
    The answer involving SourceFileLoader should give you what you want, but in the long term you should really learn how to make and use packages. Commented Nov 25, 2020 at 11:48
  • What would be then a solution using packages? Commented Nov 25, 2020 at 11:55

2 Answers 2

2

You can use SourceFileLoader from importlib.machinery to import from a path.

So you can use:

# file2.py
from importlib.machinery import SourceFileLoader
PATH = "../folder1/file1.py"
file1 = SourceFileLoader("module.name", PATH).load_module()

Or if you would import from a package then you can use:

# file2.py
from folder1.file1 import func

If you would like to use the package approach then you will need to run it from main_folder.

answered Nov 25, 2020 at 11:37
Sign up to request clarification or add additional context in comments.

Comments

0

Also you can add absolute path to sys.path.

sys.path

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

A program is free to modify this list for its own purposes. Only strings and bytes should be added to sys.path; all other data types are ignored during import.

import sys
from pathlib import Path
sys.path.append(str(Path(sys.path[0]).resolve().parent / "folder1"))
import file1
answered Nov 25, 2020 at 12:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.