Sairam Mangeshkar
Sairam Mangeshkar

Reputation: 371

ModuleNotFoundError: No module named 'xxx'; 'xxx' is not a package

I am creating a project called OpenCity which contains a premium version. The premium package is the problem.

The project hierarchy is:

opencity // is a project in PyCharm 2019.3
      premium // is a package
             __init__.py // is a module
             premium.py // is a module
             premium_user.py // is a module
             premium_user.txt // is a text file

premium_user.py:

import random as ran


def premium_users_adder():
    premium_user1a = open('premium_user.txt', 'a')
    # premium_user1w = open('premium_user.txt', 'w')
    # premium_user1r = open('premium_user.txt', 'r')
    p2 = int(input("How many members do you want to add to premium_users to database? "))
    p1 = []
    p3 = []
    for i in range(p2):
        member = input("Type the person's name. ")
        p1.append(member)
        id1 = ran.randint(100000000, 99999999999)
        p3.append(str(id1))
    for i, e in zip(p1, p3):
        premium_user1a.write(i + ' ' + e + '\n')
    premium_user1a.close()
    print()
    print("Done")


def premium_users_checker():
    premium_user1r = open('premium_user.txt', 'r')
    p2 = []
    for data in premium_user1r:
        p2.append(data)
        print(p2)

premium.py:

import premium.premium_users as pu
pu.premium_users_adder()
pu.premium_users_checker()

Error when running premium.py:

Traceback (most recent call last):
  File "F:/PyCharm Python Works/OpenCity/premium/premium.py", line 1, in <module>
    import premium.premium_users as pu
  File "F:\PyCharm Python Works\OpenCity\premium\premium.py", line 1, in <module>
    import premium.premium_users as pu
ModuleNotFoundError: No module named 'premium.premium_users'; 'premium' is not a package

I have given everything except the premium_users.txt which contains premium codes.

Upvotes: 1

Views: 13692

Answers (2)

Tanvir Islam Streame
Tanvir Islam Streame

Reputation: 460

you can also use from then import any module

do this-

from premium import premium_users as pu

instead of-

import premium.premium_users as pu

Upvotes: 0

user3980558
user3980558

Reputation:

These errors occur because your local module named premium.py shadows the installed premium module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name (You can read more about how python finds packages here).

An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:

Notice the name you used in your script:

File "F:/PyCharm Python Works/OpenCity/premium/premium.py", line 1, in <module>

The module you are trying to import: premium

Rename your module to something else to avoid the name collision.

Python may generate a premium.pyc file next to your premium.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, reproducing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.

Upvotes: 2

Related Questions