Python3 – Trying to import module from other folder with sys not working. How to fix it?

Question:

I’m using Python3 on Windows. I have the following file structure:

enter image description here

This is the folder path when I copy it from windows:

D:5 - CodingBack-endCreateGameskato_utils 

I’m trying to import the full module:

kato_utils.py

into:

main.py

But it does not work.
Here are the path I tried in my main.py but none works:

import sys
sys.path.insert(1, "D:/5 - Coding/Back-end/CreateGames/kato_utils")
# sys.path.insert(1, "D:5 - CodingBack-endCreateGameskato_utils")
# sys.path.insert(1, "D:\5 - Coding\Back-end\CreateGames\kato_utils")
import kato_utils
from kato_utils import kato_utils

How to import kato_utils.py or any of his functions into my main.py?

Asked By: Kato Design TM

||

Answers:

if you put an __init__.py file inside the kato_utils folder you wouldn’t have to worry about using the sys.path you could just use

from kato_utils import kato_utils 

As it stands now this:

import kato_utils 

should work and the second from

kato_utils .... 

is unnecessary

Answered By: Alexander
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.