PedroBiel
PedroBiel

Reputation: 509

I can't import a class from a folder at another level

My directory hierarchy is:

myproject
    |
    |----classes
    |        |
    |        |----__init__.py
    |        |----myclasse.py
    |
    |----scripts
    |        |
    |        |----__init__.py
    |        |----main.py

I have attempted to import the method 'meth' from the class 'myclasse.py' into 'main.py as follows:

from ..classes.myclasse import meth

or

from ..classes import myclasse
from myclasse import meth

or

import ..classes.myclasse
from myclasse import meth

However, none of my attempts have been successful.

I have done my own research on similar questions, but I do not understand why this is not functioning as I would like.

Upvotes: 1

Views: 81

Answers (2)

kingledion
kingledion

Reputation: 2500

Adding onto Alexey, here is some background reading, Section 6.4 in particular: https://docs.python.org/2/tutorial/modules.html

Also a personal experience: there are two underscores before and after __init__.py.

Upvotes: 0

Alexey Smirnov
Alexey Smirnov

Reputation: 2613

Add emply __init__.py as a file in folder myproject. From mail.py issue

from myproject.classes.myclasse import meth

Upvotes: 4

Related Questions