'AttributeError: module 'usbiss' has no attribute Aclass' when creating class instance - python

Trying to write a python package and I cant create an instance of a class in one of my source files.
package layout is:
-packagedir
----README.md
----setup.py
----packagename
--------__init__.py
--------package.py
--------modules
------------file1.py
------------file2.py
in init.py within packagename i have:
from . modules import file1
from . modules import file2
The file file1.py contains a class:
class File1():
def __init__(self):
self.val = 0
# Other methods and such
The file file2.py contains a class:
class File2():
def __init__(self):
self.type = 0
# Other methods and such
and in package.py I have a class as thus:
class Aclass(file1.File1, file2.File2):
def __init__(self):
# nothing important in here yet
I have build and installed my package like this:
python3 setup.py sdist
sudo pip3 install dist/package-0.1.tar.gz
Now I create a file called test.py and put in it the following:
import package
iss = package.Aclass()
when I run the test file i get the following error:
AttributeError: module 'usbiss' has no attribute 'Aclass'
I do not understand why it is that python is not letting me create an instance of class Aclass and thinks I am accessing an attribute. I am sure there is something fundamentally wrong with my import statements or something but i am at a loss as to what it is. How do I correct this so that I can create an instance of Aclass and use its methods?
Thanks.

The problem here was that I was importing the package itself but not a module within that package. I changed my import in test.py to:
from package import package
and this fixed my issue.

Are you sure you are handling your import properly and not introducing any circular dependencies?
Also:
def __init__(file1.File1, file2.File2):
def __init__():
Your init methods are lacking self. They should be:
def __init__(self, file1.File1, file2.File2):
def __init__(self):

Related

Redundant classname references in Python

My python program has the following structure
projectfolder
|--classes
|--__init__.py
|--Exampleclass.py
|--singletons
|--__init__.py
|--Examplesingleton.py
__init__.py
Main.py
Basically, in my main class, I want to create several instances of Exampleclass.py (and other classes from the folder classes) and put them inside an instance of Examplesingleton.py (basically the "World" that is populated by the instances of classes).
The problem is, I always have to call Examplesingleton and Exampleclass like this:
Main.py
import singletons.Examplesingleton as Examplesingleton
import classes.Exampleclass as Exampleclass
test = Exampleclass.Exampleclass("test")
Examplesingleton.Examplesingleton.Exampleclasses.append(test)
Is there a way in Python to reference these classes without typing the Class name twice?
Instead of
Exampleclass.Exampleclass()
I want to write just
Exampleclass()
I tried using
from classes.Exampleclass import Exampleclass
seems to enable that but leads to circular import error messages.
Exampleclass.py:
class Exampleclass:
Somevariable = 0
def __init__(self, somevariable):
self.Somevariable = somevariable
Examplesingleton.py:
class Examplesingleton(object):
Exampleclasses = []
#classmethod
def __init__(cls):
pass

Python import on sub-folders

It`s been a while since I create my classes and import them to my script, using
from <folder> import <file>
and my file.py looks like this:
class myClass:
def __init__():
and so on.
However, whenever I want to use this class on my main script, I have to do:
file.myClass()
Is thera a better way so I could use only "myClass()"?
I have recreated the scenario with the following directory structure:
.
├── outer.py
└── some_folder
└── inner.py
You missed the self in __init__ method:
some_folder/inner.py:
class myClass:
def __init__(self):
print("myClass is initiated")
Import the class from the file when you want to directly use the class name.
outer.py:
from some_folder.inner import myClass
some_object = myClass()
Output:
myClass is initiated
Instead of importing the file, you can import the class
from package.module import MyClass

How to import one class from many classes on one python file

Here is is the directory structure:
->src/dir/classes.py
->src/run.py
# classes.py
class A():
def methA():
# class A
class B():
def MethB():
# class B
class C():
def methC():
# class C
then i need to import Class A in run.py file.
from dir.classes import A
A.methA()
i already tried with using from dir.classes import A but it gives me
ModuleNotFoundError: No module named 'dir.classes'; 'classes' is not a package error
So how can i do that?
You need to put__init__.py file on your dir folder.
This way dir will be recognized as python package.
First, you must have __init__.py in each directory for Python to recognize them as packages.
Then, you should use from dir.classes import A. A is the name of the class, you shouldn't use Class A

Testing class initializer using unittest in python

I am using unittest module for writing tests.
I need to test initialization of the object inside a testcase using different inputs.
For this purpose I am importing the class inside setUp(). But when I try to use the class inside test_*() functions, I get this error - NameError: name 'Example' is not defined
Here is my code sample-
import unittest
class TestExample(unittest.TestCase):
def setUp(self):
import Example
def test_sample_function(self):
e = Example(1,2)
I know that I can simply import the class at top of the script. But I do not want to do that. I need to import it only during setup of the testscript.
Looking for some help here.
import unittest
class TestExample(unittest.TestCase):
def setUp(self):
import Example
self.Example = Example
def test_sample_function(self):
e = self.Example(1,2)
There's no reason to import the module in setUp. The module is still available globally in sys.modules, but you've only bound it to a local name that goes away after setUp returns. Just import it globally.
import unittest
import Example
class TestExample(unittest.TestCase):
def test_sample_function(self):
e = Example(1,2)

Python nested package issue

I have been trying to fix this for a bit, and I must be missing something basic here (forgive me, I am relatively new to Python development):
I have a package structure like this:
base
|
-->util
__init__.py
Class1.py
Class2.py
__init__.py
Main.py
My classes are fairly benign:
class Class1(object):
def __init__(self):
# some methods...
class Class2(object):
def __init__(self):
# more methods...
In the Main.py, I have:
import utils
if __name__ == '__main__':
c1 = utils.Class1()
c2 = utils.Class2()
My PYTHONPATH is setup to include src, src\base, and src\base\utils. But, Python gives me this error when trying to run Main.py:
AttributeError: 'module' object has no attribute 'Class1'
Has someone encountered this, and do you know how to fix it? Thanks!
This is a little different than Java. In java each file is usually a class, in python, each file is a module. Given the scenario you describe here, you would do:
import utils.Class1
import utils.Class2
if __name__ == '__main__':
c1 = utils.Class1.Class1()
c2 = utils.Class2.Class2()
You could do a number of things here. For example, you could have a module called "resources" like this:
base ->
utils ->
resources.py
which contains both Class1 and Class2. Then you could do something like:
import utils.resources
c1 = utils.resources.Class1()
etc. But the key is that classes != files in python
did you import your classes into the main.py of the utils module?
just add to utils/init.py:
from Class1 import Class1
from Class2 import Class2
then in your main.py, "import utils" will import those files as utils.Class1 etc

Categories