0

Im pretty new to python, and have been having a rough time learning it. I have a main file

import Tests
from Tests import CashAMDSale
CashAMDSale.AMDSale()

and CashAMDSale

import pyodbc
import DataFunctions
import automa
from DataFunctions import *
from automa.api import *
def AMDSale():
 AMDInstance = DataFunctions.GetValidAMD()

And here is GetValidAMD

import pyodbc
def ValidAMD(GetValidAMD):
 (short method talking to a database)

My error comes up on the line that has AMDInstance = DataFunctions.GetValidAMD()

I get the error AttributeError: 'module' object has no attribute 'GetValidAMD'

I have looked and looked for an answer, and nothing has worked. Any ideas? Thanks!

asked Jun 26, 2015 at 16:39
2
  • Can you post the source code of the DataFunctions module? Commented Jun 26, 2015 at 16:45
  • DataFunctions is the folder i have GetValidAMD in Commented Jun 26, 2015 at 16:45

3 Answers 3

1

DataFunctions is a folder, which means it is a package and must contain an __init__.py file for python to recognise it as such.

when you import * from a package, you do not automatically import all it's modules. This is documented in the docs

so for your code to work, you either need to explicitly import the modules you need:

import DataFunctions.GetValidAMD

or you need to add the following to the __init__.py of DataFunctions:

__all__ = ["GetValidAMD"]

then you can import * from the package and everything listen in __all__ will be imported

answered Jun 26, 2015 at 17:02
Sign up to request clarification or add additional context in comments.

Comments

1

When you create the file foo.py, you create a python module. When you do import foo, Python evaluates that file and places any variables, functions and classes it defines into a module object, which it assigns to the name foo.

# foo.py
x = 1
def foo():
 print 'foo'
>>> import foo
>>> type(foo)
<type 'module'>
>>> foo.x
1
>>> foo.foo()
foo

When you create the directory bar with an __init__.py file, you create a python package. When you do import bar, Python evaluates the __init__.py file and places any variables, functions and classes it defines into a module object, which it assigns to the name bar.

# bar/__init__.py
y = 2
def bar():
 print 'bar'
>>> import bar
>>> type(bar)
<type 'module'>
>>> bar.y
2
>>> bar.bar()
bar

When you create python modules inside a python package (that is, files ending with .py inside directory containing __init__.py), you must import these modules via this package:

>>> # place foo.py in bar/
>>> import foo
Traceback (most recent call last):
 ...
ImportError: No module named foo
>>> import bar.foo
>>> bar.foo.x
1
>>> bar.foo.foo()
foo

Now, assuming your project structure is:

main.py
DataFunctions/
 __init__.py
 CashAMDSale.py
 def AMDSale(): ...
 GetValidAMD.py
 def ValidAMD(GetValidAMD): ...

your main script can import DataFunctions.CashAMDSale and use DataFunctions.CashAMDSale.AMDSale(), and import DataFunctions.GetValidAMD and use DataFunctions.GetValidAMD.ValidAMD().

answered Jun 26, 2015 at 16:57

1 Comment

I should have clarified my folder structure, sorry! Its main.py DataFunctions/ __init__.py GetValidAMD.py Tests/ __init__.py CashAMDSale.py
0

Check out this.

It's the same problem. You are importing DataFunctions which is a module. I expct there to be a class called DataFunctions in that module which needs to be imported with

from DataFunctions import DataFunctions
...
AMDInstance = DataFunctions.GetValidAMD()
answered Jun 26, 2015 at 16:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.