Calling static method - python

I'm trying to call a static method of a class from a different module and getting:
AttributeError: ClassObject instance has no attribute 'foo1'
Things are structures like this:
a.py file content:
class Conf():
def __init__(self,......):
.
.
.
#staticmethod
def foo1():
.
.
.
b.py file content:
from a import Conf
Conf.foo1()
What am I doing wrong?

You are calling your method in the good way, so maybe you are not importing the module.
Check which file is loaded as a.py in b.py:
import a
print a.__file__
This will print which file is loaded.

Related

pytest mock variable calling the actual method

I have two files foo.py and bar.py
foo.py
NAME = os.getenv('NAME').lower()
bar.py
from foo import NAME
def helper():
print (NAME)
I have a test file test/test_name.py
import bar
#mock.patch('bar.NAME', "Alice")
def test__get_file(monkeypatch):
print(bar.NAME)
Giving an error:
../bar.py:6: in <module>
from foo import NAME
../foo.py:17: in <module>
NAME = os.getenv('NAME').lower()
E AttributeError: 'NoneType' object has no attribute 'lower'
What I am missing?
A per os module documentation, os.getenv can return a default value if the environment variable does not exist.
So, you could modify foo.py to avoid raising an error when running your test (for the reason given by #MrBeanBremen in his comment) like this:
NAME = os.getenv('NAME', default="MISSING").lower()

Importing function from program in a subdirectory

Using python I launch a script from a certain point. From here, there's the subdirectory "A" where it's contained the file "B.py", which contains a class called "C"
from A import C
Traceback
ImportError: cannot import name 'C' from 'A' (unknown location). Is there an easy way to make python look in A then in B and finally get C? Thank you
If I have a directory structure like the following:
A -----------|
|- B.py
test.py
B.py contains the following code:
class C:
def __init__(self):
print("I made it to C!")
If I'm writing test.py to need an instance of C I need to import like this:
from A.B import C
myInst = C()
And I get the following output when I run test.py
I made it to C!
Alternatively, you can add the path to the directory containing B.py to your PATH with the sys module. To show this, I have put the file E.py in the structure of A/B/C/D/E.py along with an empty file named __init__.py
'=
E.py contains the following code:
class F:
def __init__(self):
print("I made it to C!")
test.py contains the following code:
import sys
sys.path.insert(0, "./A/B/C/D/") # You can put the absolute path
from E import F
myInst = F()
And I get the output:
I made it to C!

Importing a subclass where it is under a package: ModuleNotFoundError

./package/test.py is working smoothly. I am expecting ./test.py would work smoothly just like that, but I am getting this running ./test.py:
Traceback (most recent call last):
File "test.py", line 2, in <module>
from package.subclass import Subclass
File ".../package/subclass.py", line 1, in <module>
from subclass import Subclass
ModuleNotFoundError: No module named 'subclass'
It is able to import class1. When it reads the 1st line of the subclass, it gives ModuleNotFoundError.
I have tried with ./package/__init__.py, a empty one gives the same error as above. When I have proper imports in ./package/__init__.py, the error becomes not being able to even find class1 in line 1 of __init__.py.
File directory as below:
./package/class1.py
./package/class2.py
./package/subclass.py
./package/test.py
./test.py
Code:
# ./package/class1.py
class Class1():
...
# ./package/class2.py
class Class2():
...
# ./package/subclass.py
from class2 import Class2
class Subclass(Class2):
...
# ./package/test.py
from class1 import Class1
from subclass import Subclass
...
# ./test.py
from package.class1 import Class1
from package.subclass import Subclass
...
Setting PYTHONPATH could be an option. Alternatively, given your current structure:
#subclass.py
from .class2 import Class2
class Subclass(Class2):
pass
#package/test.py
from .class1 import Class1
from .subclass import Subclass
#test.py
from package.class1 import Class1
from package.subclass import Subclass
To run test.py, simply run python test.py or python -m test
To run package/test.py, you will have to use python -m package.test
Try adding an empty __init__.py to the directory
The __init__.py tells the python interpreter that the directory it is dealing with is actually a module.
Hope this helps!
Aside from proper __init__.py, you'll also need to use:
from package.class1 import Class1
from package.subclass import Subclass
Note the added package.. Just a dot would also work, as that would be a relative import.

Issue with importing my classes

Consider that I have 3 files.
I am trying to put different import function in one file and call that file i.e., File2 in this case directly.
File1.py
class generalTools():
def waitAppear():
wait()
File 11.py
class machinedata():
def max():
maxtime()
File2.py
import File1
import File11
self.generalTools = File1.generalTools()
self.machinedata=machinedata.max()
File3.py
from File2 import *
Note here I am not creating an object; just trying to call File1->waitAppear()
self.generalTools.waitAppear(self)
Whenever I execute above code, it throws error saying "generaTool has no instance"
What's wrong with the above code?

Python modules importing

I have folder with such structure:
parent/
---__init__.py
---SomeClass.py
---Worker.py
First file (__init__.py) is empty.
Second file (SomeClass.py) content is following code:
class Test:
pass
Third file (Worker.py):
import SomeClass
Test()
ImportError: No module named SomeClass
What I do wrong?
Try
from . import SomeClass
but remember you'll have to
SomeClass.Text()
instead of just Test()

Categories