0

I create the following package in eclipse via PyDev:

class Repository(object):
    '''
    classdocs
    '''

    def __init__(self):
        '''
        Constructor
        '''
        print("salaam")

class Materials(Repository):
    '''
    '''

    def __init__(self):
        '''
        constructor
        '''

My main file is:

if __name__ == '__main__':
    pass

import repository;


x = Repository();

When i run my application, i get the following error:

x = Repository();

NameError: name 'Repository' is not defined

Of course, i got a warning on importing my module.

I know my import and relation of my main file and my package or eclipse configuration have problem.

My <code>dir struct</code> of my <code>project</code>

PersianGulf
  • 2,845
  • 6
  • 47
  • 67

2 Answers2

3

first of all, when you import like this, you can only refer to your class as either repository.Repository or repository.repository.Repository, depending on the whether you import the module or the package.

second, what you import depends on where eclipse thinks you are. You can check that with

import os
print(os.pwd)

at the top of your main script.

third, if you want to import your package like this, you should put it in your search path. You can do that by placing it in site-packages, or for instance by adding

import sys
import os
sys.path.append(os.path.abspath(__file__))

at the top of your main script

additionally, you might want to avoid confusion by giving your module a different name than the package (or the other way round)

(and a little nitpick: __init__ is not the constructor, merely an initializing routine).

Albert Visser
  • 1,124
  • 6
  • 5
2

The import is wrong.

Instead of

import repository

you want to write for your case:

from repository.repository import Repository

As for PyDev giving the error, it's correct at this point and when you fix your code it should stop complaining.

Fabio Zadrozny
  • 24,814
  • 4
  • 66
  • 78