ModuleNotFoundError when trying to import module from the same package - python

I have an import problem which is probably obvious, but I'm kind of stuck, so I will ask here, maybe you will see it immediately:
files structure:
foobar_package/
__init__.py
classes.py
enums.py
classes.py:
import enums
class Foo:
...
I want Foo class to be importable directly from the package:
Instead of:
from foobar_package.classes import Foo
I want to be able to write:
from foobar_package import Foo
So I edited my init.py:
__init__.py:
from .classes import *
The problem is, when do:
import foobar_package
I get:
File "foobar_package/__init__.py", line 2, in <module>
from .classes import *
File "foobar_package/classes.py", line 4, in <module>
import enums
ModuleNotFoundError: No module named 'enums'
Any idea?

Related

Create a python module with correct import arguments

Im trying to better understand the structure of a module that is imported with init.py files so I can apply it to my project.
I have this structure:
+ process.py
/packagename
/subpackage
+ __init__.py
+ subscript1.py
+ __init__.py
+ script1.py
+ script2.py
process.py:
import packagename
script1.py:
from script2 import Script2Class
script2.py:
from subpackage.subscript1 import SubScript1Class
init.py of /packagename :
from .subpackage import *
from .script1 import *
from .script2 import *
init.py of /subpackage:
from .subscript1 import *
Question1
When I import packagename it gives me this error:
File "process.py", line 1, in <module>
import packagename
File "C:\Users\du-mul\PycharmProjects\PakageTesting\packagename\__init__.py", line 4, in <module>
from .script1 import *
File "C:\Users\du-mul\PycharmProjects\PakageTesting\packagename\script1.py", line 1, in <module>
from script2 import Script2Class
ModuleNotFoundError: No module named 'script2'
That happens with script2.py too: He cant find the subpackage.
Why does Script1 not find Script2 even though they are in the same folder?
I can fix that mistake my applying this:
from packagename.script2 import Script2Class
However, then I cant execute script2.py on its own.
How should I write the imports correctly?
Question2
Do I need to import Script2 in the first init file in the first place? (Since only Script1 is using it)
Question3
When I import the subpackage there are 2 ways that work :
from .subpackage1 import *
or
import packagename.subpackage1
Which one should I use?
Do both import arguments make use of the subpackages init file?
Any help is appreciated

import in python file has imported file

I have python project like
/project
|__main.py
|__/graph
|____grapher.py
|____object_map_genaration.py
In grapher: import object_map_genaration
In main: import grapher
But I got this error:
ModuleNotFoundError: No module named 'object_map_genaration'
First add an init here:
/project/
|__/main.py
|__/graph/
|____/__init__.py
|____/grapher.py
|____/object_map_genaration.py
Then in grapher.py:
from . import object_map_genaration
And in main.py:
from graph import grapher
You need from grapher import object_map_genaration not just import object_map_genaration, since imports are relative to the root of the main file.

Structuring a python project

I need to know how to set up the __init__.py and imports in order to structure a python project where I can use fully qualified names throughout the package.
The package will contain a number of sub packages which may contain clashing names. The classes contained within the package will sub class each other and contain references to each other. The project will be generated so the use of fully qualified names would make life a lot simpler.
This sample project represents the structure I am aiming at, but only contains a single sub project, while the IDE seems happy with it fails when its run.
MyPackage/__init__.py
import SubPackage as SubPackage
MyPackage/SubPackage/__init__.py
from .FileB import ClassB
from .FileA import ClassA
MyPackage/SubPackage/FileA.py
from __future__ import absolute_import
import MyPackage
class ClassA(MyPackage.SubPackage.ClassB):
thingA: 'MyPackage.SubPackage.ClassA'
thingB: MyPackage.SubPackage.ClassB
def __init__(self):
self.thingA = None
self.thingB = None
def test(self):
self.thingA = MyPackage.SubPackage.ClassA()
self.thingB = MyPackage.SubPackage.ClassB()
MyPackage/SubPackage/FileB.py
from __future__ import absolute_import
import MyPackage
class ClassB(object):
nextB: 'MyPackage.SubPackage.ClassB'
def __init__(self):
self.nextB= None
def test(self):
self.nextB= MyPackage.SubPackage.ClassB()
test.py
import MyPackage
x = MyPackage.SubPackage.ClassA()
Error
File "C:\temp\Test.py", line 3, in <module>
import GeneratedLx
File "C:\temp\MyPackage\__init__.py", line 1, in <module>
import Bs as Bs
File "C:\temp\MyPackage\SubPackage\__init__.py", line 12, in <module>
from .FileA import ClassA
File "C:\temp\MyPackage\SubPackage\FileA.py", line 5, in <module>
class ClassA(MyPackage.SubPackage.ClassB):
AttributeError: module 'MyPackage' has no attribute 'SubPackage'
You already cannot have name conflicts at the SubPackage level, so adding MyPackage is entirely redundant, and doesn't work quite the way you're trying to use it. This may be due to when names are bound or something else, but ultimately there should be no instance when you need it. This leaves you to slightly edit the files: "FileA.py", and "FileB.py":
FileA.py
from __future__ import absolute_import
from MyPackage import SubPackage
class ClassA(SubPackage.ClassB):
thingA: 'SubPackage.ClassA'
thingB: SubPackage.ClassB
def __init__(self):
self.thingA = None
self.thingB = None
def test(self):
self.thingA = SubPackage.ClassA()
self.thingB = SubPackage.ClassB()
FileB.py
from __future__ import absolute_import
from MyPackage import SubPackage
class ClassB(object):
nextB: 'SubPackage.ClassB'
def __init__(self):
self.nextB= None
def test(self):
self.nextB= SubPackage.ClassB()
The import statement is also equivalent to from .. import Subpackage using relative imports rather than absolute if desired. Stylistically I tend to use relative imports to help me quickly pick out which imports are part of my project, and which ones are external dependencies.

How to import module without class in Python?

I have 4 files in my project:
project/__init__.py
project/app.py
project/mod_x.py
project/mod_y.py
In mod_x.py I have a class (e.g. ModX)
In mod_y.py I have just one function.
I import modules from app.py as follows:
from .mod_x import ModX
import .mod_y
I get an error:
ImportError: No module named 'mod_y'
Before I created init.py I didn't have that kind of problems (of course, I dont put "." before module name).
How to import module which doesn't have the class inside in Python3 with init.py file inside the current directory?
Relative imports are only available for from...import syntax.
You could import that function this way:
from .mod_y import FUNCTION_NAME
Module could be imported this way:
from . import mod_y

how to import the blog.py(i import the 'blog' folder)

my dir location,i am in a.py:
my_Project
|----blog
|-----__init__.py
|-----a.py
|-----blog.py
when i 'from blog import something' in a.py , it show error:
from blog import BaseRequestHandler
ImportError: cannot import name BaseRequestHandler
i think it import the blog folder,not the blog.py
so how to import the blog.py
updated
when i use 'blog.blog', it show this:
from blog.blog import BaseRequestHandler
ImportError: No module named blog
updated2
my sys.path is :
['D:\\zjm_code', 'D:\\Python25\\lib\\site-packages\\setuptools-0.6c11-py2.5.egg', 'D:\\Python25\\lib\\site-packages\\whoosh-0.3.18-py2.5.egg', 'C:\\WINDOWS\\system32\\python25.zip', 'D:\\Python25\\DLLs', 'D:\\Python25\\lib', 'D:\\Python25\\lib\\plat-win', 'D:\\Python25\\lib\\lib-tk', 'D:\\Python25', 'D:\\Python25\\lib\\site-packages', 'D:\\Python25\\lib\\site-packages\\PIL']
zjm_code
|-----a.py
|-----b.py
a.py is :
c="ccc"
b.py is :
from a import c
print c
and when i execute b.py ,i show this:
> "D:\Python25\pythonw.exe" "D:\zjm_code\b.py"
Traceback (most recent call last):
File "D:\zjm_code\b.py", line 2, in <module>
from a import c
ImportError: cannot import name c
When you are in a.py, import blog should import the local blog.py and nothing else. Quoting the docs:
modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script
So my guess is that somehow, the name BaseRequestHandler is not defined in the file blog.py.
what happens when you:
import blog
Try outputting your sys.path, in order to make sure that you have the right dir to call the module from.

Categories

Resources