How to build a package out of a class
Question: I have written a class called editClass which works fine. The class is
completely defined in the file editClass.py. The constructor is given by:

    
    
    def __init__(self,filename):
        self.File=filename
    

I want now to build a package that only contains this class. In future of
course also other classes... I spend some time on google and here searching
how exactly this works but I have reached some kind of dead end. What I have
so far are the following files

    
    
    setup.py
    edit/__init__.py
    edit/editClass.py
    

The class definition in editClass remained untouched and works. The init only
contains: from editClass import editClass and the setup is

    
    
    from setuptools import setup
    
    setup(name='edit',
         version='0.1.0',
         packages=['edit'],
         author='my name'
         )
    

I installed this with $python setup.py install in the shell. This seemed to
work. In a test file test.py I want to call the constructor of editClass. But

    
    
    import edit
    filename= "name.txt"
    test=edit.editClass(filename)
    

does not work. The error massage is:

AttributeError: 'module' object has no attribute 'editClass'

I do not know if I referred to the constructor worngle or if the problem is
already in the installed package. I to be honest, I don't even know how to
check.

edit: Since from edit import editClass does nor work either, I believe the
problem is in the definition of the package.


Answer: Given your code structure, you need to change

    
    
    import edit
    filename= "name.txt"
    test=edit.editClass(filename)
    

to

    
    
    import edit.editClass
    filename = "name.txt"
    test = edit.editClass.editClass(filename)
    

What you've actually got is a package called `edit`, containing a module
called `editClass` containing a class called `editClass`. So your import needs
to import the module (`edit.editClass`) and you then need to qualify the class
with it's module name. Alternatively you can do

    
    
    from edit.editClass import editClass
    filename = "name.txt"
    test = editClass(filename)
    

On the other hand, if what you were trying to achieve was for your client code
to look as you had it, then you need to put the `editClass` class definition
into a file called `edit.py`.

