Kemsa
Kemsa

Reputation: 53

package import and NameError

I have some troubles with importation of self-made modules, I just can't see what I am doing wrong.

I have a package named basics, which has all my base classes I have a second package named components, and every module in componentsuses the modules from basics. I have a script file, located in another folder, which calls upon the basics and components modules.

I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "H:/scripts/CIF_utilities/scripts/hello world.py", line 11, in <module>
    TW=TextWriter(r'H:/scripts/CIF_utilities/components')
  File "H:\scripts\CIF_utilities\components\textwriter.py", line 23, in __init__
    layout=Layout(File=os.path.join(path,'alphabet.CIF'))
NameError: global name 'Layout' is not defined

There is my script: hello world.py

#hello world.py
import basics
from components.textwriter import *

TW=TextWriter(r'H:/scripts/CIF_utilities/components')

cell=TW.writeText('Hello World',30e3)
cell.draw()

layout=Layout()
layout.addCell(cell)
layout.workCell=cell

layout.exportCIF('hello world',os.getcwd())

textwriter.py is the one giving the error. In init, I load some data from a preformatted file using the Layout class (which will make the import) in textwriter.py

#texwriter.py
import basics
import os, os.path, sys
import re
from numpy import *
from scipy import *


class TextWriter:

    def __init__(self,pathToCIF=None):
        if pathToCIF==None:
            path=os.path.split(textwriter.__file__)[0]
        else:
            path=pathToCIF            

        ###line that crashes is here        

        layout=Layout(File=os.path.join(path,'alphabet.CIF'))
        self.alphabet=layout.workCell

There is the layout.py class:

#layout.py
import basics
from numpy import *
from scipy import *
import Tkinter
import tkFileDialog
import os, os.path
import re
import datetime

class Layout:
    countCell=0
    @classmethod
    def getNewNumber(self):
        Layout.countCell+=1
        return Layout.countCell


    def __init__(self,File=None):
        self.cellList=[]
        self.layerList=[]
        self.nameFile=""
        self.comments=""
        self.workCell=None

        if File!=None:
            self.importCIF(File)

the init.py of the basics package contains all the necessary importations:

#__init__.py in basics folder
from baseElt import *
from cell import *
from layout import *
from transformation import *

the init.py from components is empty

I am currently using the anaconda 64bits distribution (python 2.7 if I recall well)

Thanks for your much needed help!

Upvotes: 0

Views: 2306

Answers (2)

bitsplit
bitsplit

Reputation: 1060

In textwriter.py, you want to switch

layout=Layout(File=os.path.join(path,'alphabet.CIF'))

for

layout=basics.Layout(File=os.path.join(path,'alphabet.CIF'))

You may have similar problems along your code. it is good to note that it is not pythonic to use

from package import *

It is recommended to instead use

from package_or_module import specific_item
import package_or_module

Remember, if you import with regular import you have to use the module/package name to access the object you want (i.e. basics.Layout).

Upvotes: 0

chepner
chepner

Reputation: 531185

Since Layout is imported in basics/__init__.py, it only exists in the basics namespace, not in helloworld.py. Either access it with

layout = basics.Layout()

or explicitly import Layout into helloworld.py with

from basics import Layout

Upvotes: 1

Related Questions