Jivesh Bisht
Jivesh Bisht

Reputation: 29

Unable to import a file in python

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

Upvotes: 1

Views: 49

Answers (1)

Cannon Farr
Cannon Farr

Reputation: 11

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.

Upvotes: 1

Related Questions