import in python file has imported file - python

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.

Related

ModuleNotFoundError while importing from alias

e.g.
import os as my_os
import my_os.path
ModuleNotFoundError: No module named 'my_os'
but the following script is ok
import os
import os.path
You can't do that in Python.
import statements are importing from Python file names.
You aren't renaming the file of os to my_os, therefore this wouldn't work.
As mentioned in the documentation:
The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope.
Tried the similar way as os.py did:
import json as my_json
from my_json.decoder import * # ModuleNotFoundError: No module named 'my_json'
import sys
import json.decoder as my_decoder
sys.modules['my_json.decoder'] = my_decoder
from my_json.decoder import * # it's ok now

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)

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

ImportError: No module named phpoob.bank

lcl
|
|----|
|----enterprise
|----phpoob
|----|----|
|----|----'bank.py'
|----|
|----'__init__.py'
|----'module.py'
this is my file structure
__init__.py-->
from module import LCLModule
__all__ = ['LCLModule']
module.py-->
from phpoob.bank import something
__all__ = ['LCLModule']
class LCLModule(something):
_code here_
these are my files
while firing the command python __init__.py i got following error ImportError: No module named phpoob.bank how shold i overcome this error
i also tried it from .phpoob.bank import something but it gives ValueError: Attempted relative import in non-package
what will be solution for it...?
Looks like you are using Python 2.x. Folder phpoob is not treated as Python module. That's why you can't import phpoob.bank.
Solution #1: Create empty file phpoob/__init__.py After that you will be able to import phpoob and import any file inside.
Solution #2: Upgrate to Python 3.

Issue with import path in Python 3

I'm having an issue with the import statement in Python 3. I'm following a book (Python 3 Object Oriented) and am having the following structure:
parent_directory/
main.py
ecommerce/
__init__.py
database.py
products.py
payments/
__init__.py
paypal.py
authorizenet.py
In paypal.py, I'm trying to use the Database class from database.py. So I tried this:
from ecommerce.database import Database
I get this error:
ImportError: No module named 'ecommerce'
so I try with both of these import statements:
from .ecommerce.database import Database
from ..ecommerce.database import Database
and I get this error:
SystemError: Parent module '' not loaded, cannot perform relative import
What am I doing wrong or missing?
Thank you for your time!
Add your parent_directoryto Python's search path. For example so:
import sys
sys.path.append('/full/path/to/parent_directory')
Alternatively, you can add parent_directory to the environmental variable PYTHONPATH.

Categories

Resources