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)
Related
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
I have a question about my new python project. It is the first time that I use different folders for my project.
I have the following structure:
project
src
securityFunc
__init__.py
createCredentialsXML.py
main.py
I work in PyCharm environment.
After pressing Play i get the error message:
Traceback (most recent call last):
File "C:\project\src\main.py", line 1, in <module>
from securityFunc import *
File "C:\project\src\securityFunc\__init__.py", line 1, in <module>
from createCredentialsXML import *
ModuleNotFoundError: No module named 'createCredentialsXML'
My main function looks like this:
from securityFunc import *
if __name__ == '__main__':
generate_key()
__init__.py:
from createCredentialsXML import *
createCredentialsXML.py:
def generate_key():
key = base64.urlsafe_b64encode(os.urandom(2048))
with open("../key/secret.key", "wb") as key_file:
key_file.write(key)
I tried using Path or sys.path to fix the problem. But it does not work.
Can you please tell me how to fix the problem?
Since you're doing a relative import, you need to add a . before your module name:
from .createCredentialsXML import *
The dot means the module is found in the same directory as the code importing the code.
You can read more about it here
createCredentialsXML.py works in the module securityFunc; you must specify the scope of the import. Using
from securityFunc.createCredentialsXML import *
in securityFunc.__init__.py should work.
I found this question helpful, but Python is unable still to find the folder.
I have a Django folder structure that looks like this:
todoproject
pythonDiscordChatbot
processLessons.py
todoapp
testChatFunctions.py
I'm trying to import a function called 'routes' into testChatFunctions.py, but I keep getting this error:
File "c:\Users\Kaij\Documents\chatSiteDjango\todoproject\todoapp\testChatFunctions.py", line 6, in <module>tSiteDjango\todoproject\todoapp\tesssLesson import *tChatFunctions.py", line 6, in <modd 'pythonDiscordChatbot'ule> atSiteDjango> & C:/Users/Kaij/Documents/djangoTests/django_tailwind_project/env/Scripts/
from pythonDiscordChatbot.procechatSiteDjango/todoproject/todoapp/testChatFunctions.pyssLesson import *
ModuleNotFoundError: No module nametSiteDjango\todoproject\todoapp\testChatFunctions.py", line 6, in <module>d 'pythonDiscordChatbot
Here's what I have tried:
# Bring your packages onto the path
import sys, os
sys.path.append(os.path.abspath(os.path.join('..', 'pythonDiscordChatbot')))
# Now do your import
from pythonDiscordChatbot.processLesson import *
# from pythonDiscordChatbot.processLesson import routes
routes("hello", "website_180.02.1")
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.
I failed to import a module from sub directory in python. Below is my project structure.
./main.py
./sub
./sub/__init__.py
./sub/aname.py
when I run python main.py, I got this error:
Traceback (most recent call last):
File "main.py", line 4, in <module>
import sub.aname
File "/Users/dev/python/demo/sub/__init__.py", line 1, in <module>
from aname import print_func
ModuleNotFoundError: No module named 'aname'
I don't know it failed to load the module aname. Below is the source code:
main.py:
#!/usr/bin/python
import sub.aname
print_func('zz')
sub/__init__.py:
from aname import print_func
sub/aname.py:
def print_func( par ):
print ("Hello : ", par)
return
I am using python 3.6.0 on MacOS
There are several mistakes in your Python scripts.
Relative import
First, to do relative import, you must use a leading dots (see Guido's Decision about Relative Imports syntax).
In sub/__init__.py, replace:
from aname import print_func
with:
from .aname import print_func
Importing a function
To import a function from a given module you can use the from ... import ... statement.
In main.py, replace:
import sub.aname
with:
from sub import print_func
from sub import aname
aname.print_func('zz')
Probably the most elegant solution is to use relative imports in your submodule sub:
sub.__init__.py
from .aname import print_func
But you also need to import the print_func in main.py otherwise you'll get a NameError when you try to execute print_func:
main.py
from sub import print_func # or: from sub.aname import print_func
print_func('zz')