M
M
Medelin02018-03-26 18:16:50
Python
Medelin0, 2018-03-26 18:16:50

How to import a Python module if it is not in the same script folder?

In the root folder there is a module and a folder with a script, tell me, please, is there a simple and elegant solution to import the module into the script?
root_folder/
subfolder/script.py
module.py

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Danil Biryukov-Romanov, 2018-03-26
@urtow

In subfolder you create an empty file with the expansion __init__.py (two underscores before and after init) and after that you can import it like this:
import subfolder.script

M
Medelin0, 2018-03-26
@Medelin0

I solved the problem through environment variables, but as I understand it, the script will not run on another computer, maybe there is a more universal option

R
Ranc58, 2018-03-26
@Ranc58

import sys
sys.path.append('..')

import module

There is such an option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question