Ilhan Ilhan - 3 months ago 17
Python Question

Python3 Cannot import function from my own module

I have browsed the site for a while and the only occasions people had this error happening were in circular imports which I don't have as far as I understand what circular imports are? My imports are transitive though.

I have 3 files in the same folder:

packer.py
parser.py
statistics.py


packer.py

class Conversation:
....
class Message:
....


parser.py (module works, called all functions from itself without a problem)

from bs4 import BeautifulSoup
from packer import Conversation
from packer import Message

def writeFormatedLog():
....
def getConvs():
....


statistics.py

from parser import getConvs #this on its own runs without problems
getConvs() #throws ImportError: cannot import name 'getConvs'

Answer

ImportErrors may happen if there are duplicate module names. Try naming your parser.py something else, since it is likely conflicting with Python's built-in parser module.

Comments