Create a python module with correct import arguments - python

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

Related

ModuleNotFoundError when trying to import module from the same package

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?

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.

Imported module fails to import sub-module

I have the following folder-structure
premier_league/
|_cli_stats/
|__ __init__.py
|__cli_stats.py
|__get_data/
|__get_stats.py
|__get_id.py
|__api_scraper/
|__api_scraper.py
In cli_stats.py I have the following import:
from get_data.get_stats import SeasonStats
In get_stats.py I have the following import:
from api_scraper.api_scraper import Football.
When running python cli_stats.py from the cli_stats folder the following error occurs.
File "cli_stats.py", line 36, in <module>
from get_data.get_stats import SeasonStats
File "/Users/name/Desktop/Projekt/premier_league_api/cli_stats/get_data/get_stats.py", line 12, in <module>
from api_scraper.api_scraper import Football
ModuleNotFoundError: No module named 'api_scraper'
But when running python get_stats.py from the get_data folder, the import is successful. Why does the import not work when running cli_stats.py from the cli_stats folder?
You have to adjust the import to a relativ one. From theget_stats.pyyou have to step into the directory. The error is that from api_scraper.api_scraper import Football is an absolut import.
Try: in get_stats.py
from .api_scraper.api_scraper import Football
(1 dot before the api_scraper)

import on Python doesn't work as expected

Although the variable should be imported, I get "name X is not defined" exception.
main.py
from config import *
from utils import *
say_hello()
utils.py
from config import *
def say_hello():
print(config_var)
config.py
from utils import *
config_var = "Hello"
Trying to run "main.py":
Traceback (most recent call last):
File "main.py", line 3, in
say_hello()
File "C:\Users\utils.py", line 3, in say_hello
print(config_var)
NameError: name 'config_var' is not defined
What happened here? Why some_var is not accessible from utils.py?
You are importing config in util and util in config which will causing this error(create cross loop). remove from utils import * from config.py and then try this.
And in main.py you don't need to import the from config import * unless you are using variables from config directly in your main()
you should also import config.config_var, since this variable belongs to that specific module
You are creating to many import statements perhaps try the following below, but also you need to define a parameter in utils.py if you are passing a parameter through there.
In utils.py we require a parameter to be passed since you want to print out the appropriate value, In config.py you are defining a value. Then in main.py as discussed before using the wildcard operator "*" isn't entirely good in this situation then in order to call the respective functions you need to address them through their file name
In utils.py :
def say_hello(config_var):
print(config_var)
In config.py
config_var = "Hello"
Then in main.py
import config as cn
import utils as ut
ut.say_hello(cn.config_var)
Check out this thread for how to write python modules as well How to write a Python module/package?

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