Home > Enterprise >  How can I fix a very basic ImportError in Python3?
How can I fix a very basic ImportError in Python3?

Time:06-13

Similar to ImportError: Cannot import name X, I have a main.py file and, in the same folder, a few other python files bill.py and flatmate.py. bill.py and flatmate.py each contain classes, nothing more.

Directory structure:

parentfolder
- main.py
- bill.py
- flatmate.py
- files
  - pic.png
  - doc.pdf

Sample code:

main.py:

import bill                     # won't work
from flatmate import Flatmate   # won't work
...

bill.py:

class Bill:
    def __init__(self, ...):
    ...              

flatmate.py:

class Flatmate:
    def __init__(self, ...):
    ...              

I get an ImportError: cannot import name 'Bill' from 'bill' (c:\Users\lavid\Desktop\Python Experimente\Python OOP Kurs\App-2-Flatmates-Bill\bill.py). I am pretty sure I don't have circular references. What could be the cause of this problem?

CodePudding user response:

Make sure that all files are next each others try:

from . import module

if this doesn't work , add your files dir to the Question

CodePudding user response:

Saving the referenced files before accessing them helped a lot.

  • Related