1

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?

2 Answers2

1

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 ;)

Harjuuso
  • 55
  • 6
  • following your instruction, I still have the problem `Traceback (most recent call last): File "Response.py", line 1, in import Logger.py ModuleNotFoundError: No module named 'Logger.py'; 'Logger' is not a package` – Sandipan Guha Feb 01 '22 at 17:18
  • Sorry i did a mistake on my part in Response.py Sorry for the inconvenience – Sandipan Guha Feb 02 '22 at 09:36
1

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)
Keto
  • 1,470
  • 1
  • 12
  • 25
  • Same problem, brother. `Traceback (most recent call last): File "Response.py", line 1, in import Logger.py ModuleNotFoundError: No module named 'Logger.py'; 'Logger' is not a package` – Sandipan Guha Feb 01 '22 at 17:20
  • 1
    Again, you're attempting to import `Logger.py`. You have to remove the extension so that it's just `import Logger`. Every file that has this mistake must be fixed as well. I think the mistake is in `Response.py`. – Keto Feb 01 '22 at 19:14
  • No sir I didn't. Whats the point of lying to you. I wrote only Logger. `# custom libraries import Logger import Response #os.system("python Logger.py") #os.system("python Response.py")` – Sandipan Guha Feb 02 '22 at 04:00
  • Either way, whatever you just commented isn't the solution I suggested but rather @Harjuuso. I recommend you update your question with the first few lines of each file. – Keto Feb 02 '22 at 04:04