Python modules importing - python

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()

Related

Python import from another file throws error

a.py
def test():
print("hi")
b.py
from a import test
test('hello')
Error:
ImportError: cannot import name 'test' from 'a' (C:\mypath\a.py)
OR b.py
import a
a.test('hello')
Error:
AttributeError: module 'a' has no attribute 'test'
Thanks in advance!
Edit: I tried to run the script in another directory on my pc so it isnt because the path or something. also, os.getcwd() doesnt work
a.py and b.py must be in the same project forlder.

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.

Calling static method

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.

Python : Cannot find any functions within my own module?

I have created my own module with filename mymodule.py. The file contains:
def testmod():
print "test module success"
I have placed this file within /Library/Python/2.7/site-packages/mymodule/mymodule.py
I have also added a __init__.py file, these have compiled to generate
__init__.pyc and mymodule.pyc
Then in the python console I import it
import mymodule
which works fine
when I try to use mymodule.testmod() I get the following error:
AttributeError: 'module' object has no attribute 'testmod'
So yeah it seems like it has no functions?
You have a package mymodule, containing a module mymodule. The function is part of the module, not the package.
Import the module:
import mymodule.mymodule
and reference the function on that:
mymodule.mymodule.testmod()
You can use from ... import and import ... as to influence what gets imported exactly:
from mymodule import mymodule
mymodule.testmod()
or
from mymodule import mymodule as nestedmodule
nestedmodule.testmod
or
from mymodule.mymodule import testmod
testmod()
etc.

Importing Python classes

I've a folder structure like this :
-PCore
|-__init__.py
|-PFnTransform.py
|-PEuler.py
|-PPoint3.py
|-...
In each .py file there is a class with the same name.
And i just want to import by writting:
from Core import PPoint3
but actually I need to write:
from Core.PPoint3 import PPoint3
And when I print the class I got <class 'PCore.PEuler.PEuler'> instead of <class 'PCore.PEuler'> (what i want)
What I need to write in the __init__.py file ?
In the __init__.py file, you need to import the PPoint3 class.
from PPoint3 import PPoint3
Then, you'll be able to import the PPoint3 class from the PCore package.

Categories