1

I want to import a file in a local directory but python refused to do so and it gives error everytime i do so.

I tried putting "from Admin_login import Admin_login" but it gives an error saying

"ModuleNotFoundError: No module named 'Admin_login'"

my code:-(main.py)

from .Admin_login import Admin_login loggsin = Admin_login.login() if loggsin == True: print("You are logged in") 

This is the Admin_login.py file

import json import os def load_data(): with open("data.json", "r") as f: Data = json.load(f) return Data class Admin_login(): def login(self): Login = False while Login == False: id = input("Enter the id you want to login = ") data = load_data() if id == data["id"]: print(data["name"]) passord = input("Enter the password = ") if passord == data["password"]: print("You are successfully logged in") Login = True return Login os.system('cls') else: print("The id doesn't exist... Please try again!") Login = False return Login os.system('cls') if __name__ == '__main__' : Admin_login() 

and the error it gives is :-

Traceback (most recent call last): File "C:\Users\myUser\Desktop\LibApp\main.py", line 1, in <module> from .Admin_login import Admin_login ImportError: attempted relative import with no known parent package 

pls help

3
  • 2
    If the two files are in the same directory, remove the . and it should work Commented May 17, 2022 at 19:26
  • do what Lecdi says above, but then you will probably get an error in main.py because that function doesn't exist. You will need to call Admin_login.Admin_login() in your first line of non-import code. Commented May 17, 2022 at 19:49
  • Can you provide the directory/file structure of your code? Commented May 18, 2022 at 7:12

1 Answer 1

1

My best guess is that the '.' is giving you the trouble. When you add a dot to a directory name, it is referring to a relative directory and not an absolute one. That's what that error you're getting is referring to. If those two files are in the same directory, you can just remove the dot and it should work.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately it ain't working for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.