1

EDIT: I couldn't use my own modules. It was a stupid waste of time. If you ever have the same problem, try reading this first: http://docs.python-guide.org/en/latest/writing/structure/

I just started OOP with Python and I am confused by modules and classes.

I work with a Mac and I can write my own modules and load them from the site-packages folder.

Now I would like to create modules with useful classes. import custom_module works. But if custom_module has a class Custom_class, things don't work.

I tried doing: (EDIT: I am sorry, I am removing old code which was made up, this is what I just used and doesn't work)

in custommodule.py:

class Customclass:
    def __init__(self, name):
        self.name = name

This module loads without errors. Then I get:

new = custommodule.Customclass('foo')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Customclass'

BTW, I started trying to do this using code from this tutorial

I was not able to get over this. Please advise, I must be doing something wrong.

15
  • 1
    What is the code for custom_module? Commented May 6, 2015 at 20:04
  • 1
    Could you edit it into your question? Commented May 6, 2015 at 20:07
  • 1
    I can't reproduce this. Are you sure that all of the files are accessible, ie: custommodule is in the same folder as the file you are working in? Commented May 6, 2015 at 20:28
  • 1
    Try to execute, in the python interpreter, the command dir (custommodule) after you import custommodule, it should print what you just imported. The code you posted seems good and it work as expected Commented May 6, 2015 at 20:28
  • 1
    Then you are importing something wrong, it should be (given your code example) ['Customclass', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__'] which is what I get trying your code Commented May 6, 2015 at 20:35

3 Answers 3

1

With this file layout

site-packages/custommodule/__init__.py
site-packages/custommodule/custommodule.py

you are creating a package named custommodule that contains a module also named custommodule. Your code needs to look like

import custommodule
# or more specifically,
# import custommodule.custommodule
new = custommodule.custommodule.Customclass('foo')

or

from custommmodule import custommodule
new = custommodule.Customclass('foo')

You can also put custommodule.py directly in site-packages to avoid creating the package, in which case your original code should work.

Sign up to request clarification or add additional context in comments.

2 Comments

>>> from utils1.utils1 import helloFoo >>> helloFoo() foo This just worked!!!!!!!!
THANKS! I didn't realise that I had to specify the package as well. Now ALL my modules work and I can call functions and classes correctly!
1

For me, at least, this works from mod_name import ClassName mod2 mod1 Runs fine

When I run this code I don't get any errors. Hope this helped

EDIT: also make sure that the module you want to import is in the project directory. If you view the left panel in the images both modules are in Stack. I hope this is obvious but, the class needs to be in the module you import as well. Make sure that the class you're importing does not import the class your importing in because then you get circular dependencies.

4 Comments

Hi, Thanks, I am not familiar with Pycharm. My modules on Mac OS are in PYTHONPATH. And I can load them without errors (they are found), but that's the only things that works. Maybe there is something wrong in the way I am organising files? Maybe I need something more than init.py?
Which IDE are you using? And your classes are created properly. It is probably something to do with the organization.
I never used an IDE. I just use Textwrangler on Mac.
That's very strange. Firstly, I'd highly recommenced an IDE like pycharm. Secondly, I'm stumped!
1

Try this way

File custommodule.py in the directory custommodule

class Customclass:
    def __init__(self, name):
        self.name = name

File __init__.py int he custommodule directory

from .custommodule import CustomClass

Note the dot before custommodule. This force the init to load the module from the same directory.

Without the dot, it work under python2 but not under python3

2 Comments

This is strange. I tried doing the import from the same directory (customodule itself!) and it worked, but it doesn't work with what you suggested. >>> from .custommodule import CustomClass Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: Attempted relative import in non-package >>> import custommodule >>> from custommodule import Customclass
You cannot do this way, since a relative import seems to be allowed only in a package. the line from .custommodule import CustomClass is in the __init__.py file, you cannot use it from the interpreter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.