Home > Enterprise >  How to import python files into main.py on repl.it?
How to import python files into main.py on repl.it?

Time:02-02

Straight to the point:

I have created three files on repl.it along with my main.py - Logger.py, Responses.py, and phrases.json

Now when I am trying to import them into main.py using,

import Logger.py
import Response.py
import phrases.json

I am getting this error:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
    import Logger.py
ModuleNotFoundError: No module named 'Logger.py'; 'Logger' is not a package

What should I do?

CodePudding user response:

I don't quite understand what you are trying to achieve.. Could you possibly attach your files here? First thing what im thinking of is, you could import/run the other files inside your main.py:

import os
os.system("python ~file~.py")

And of course replace ~file~ with your file ;)

CodePudding user response:

To import a python file from your current folder, you just need the name without extension.

import Logger  # this is ./Logger.py
import Response  # this is ./Response.py

For the JSON, you can't import these directly as they aren't python files. Python does have a standard library to read JSON:

import json

with open("phrases.json") as f:
    phrases = json.load(f)
  •  Tags:  
  • Related