I'm getting a ModuleNotFoundError while running my flask app with this dir structure:
project
| app
| __init__.py
| urls.py
| views.py
| __main__.py
In the project dir, while running python3 ., I'm getting a
File "./app/__init__.py", line 4, in <module>
import urls
ModuleNotFoundError: No module named 'urls'
The content of __init__.py is
#!/usr/bin/env python3
import logging
import flask_cors
import urls
from views import flask_app
log = logging.getLogger("werkzeug")
log.disabled = True
flask_cors.CORS(flask_app)
def run(host, port):
flask_app.run(host=host, port=port)
The code is a little big so this is the link to it.
If you want to import other files in the same directory in a __init__.py file, you need to use relative import syntax:
from . import urls
This way Python will look for urls.py alongside your __init__.py, instead of searching in sys.path.
Related
this is my file system:
source:
|_ folder1:
myfile.py
|_ folder2:
infile.py
I am currently working with "infile.py" and I want to import myfile.py from folder1, doing the following:
from ..folder1.myfile import *
result:
ImportError: attempted relative import with no known parent package
in my file I don't have any class, only functions, so I don't know if it is necessary to create a class in order to import a file as a module in Python.
The file structure for a module im creating goes as follows:
PIV
| __init__.py
| base.py
| core.py
| exceptions.py
.gitignore
LICENSE
requirements.txt
But whenever I run a file like core.py, I get the following error:
Traceback (most recent call last):
File "c:/Users/ghub4/OneDrive/Desktop/Python-Image-and-Video-tools/PIV/core.py", line 33, in <module>
from . import base
ImportError: attempted relative import with no known parent package
The same thing happens when I run the __init__.py file. I'm not sure on what went wrong because all of the python files are in the same folder. Can someone clarify what's the problem and explain how I should fix it?
Import code for core.py file:
from __future__ import absolute_import
import sys
import os
from PIL import Image
import io
from . import base
from . import exceptions
(The __init__.py folder has the same relative imports as in the core file but also including: from . import core)
Based upon the two links you will given below, here is what needed for the problem to solve:
Relative Imports error rectified
No module named base
You need to import the package as this
from mymodule import some_useful_method
Sometimes, we get the no module error, in this case, we can import like this
from module_name.classname import some_useful_method
> Project
>> __init__.py
>> moduleA
>>> __init__.py
>>> file_1.py
>> moduleB
>>> __init__.py
>>> file_2.py
I have the following Directory Structure for a Python Project. I want to import file_1.py into file_2.py . How can I do so ?
I tried inserting the given below snippet in file_2.py:
from ..moduleA import file_2
But I keep Getting this error:
ImportError: attempted relative import with no known parent package
I also tried this
from Project.moduleA import file_1
This method works in the PyCharmIDE but won't work in VSCode wherein I get this error
ModuleNotFoundError: No module named 'Project'
I'm a bit new to the Python world. I'm using Python3 and having hard times with imports.
I was using PyCharm on Windows to write an application. Everything was working through the project until I switched to Linux and VS Code.
Now I can't use absolute imports for importing modules from other packages in the same project.
For example, I'd like to import from module cards all available card types.
I tested the classes and everything is ok. I'm only getting this issue with the import stuff.
The project structure:
/
|-cards
|-__init__.py
|-card.py
|-monster_card.py
|-spell_card.py
|-trap_card.py
|-ritual_card.py
|-deck
|-__init__py
|-deck.py
|-system
# This is the code in __init__.py in cads package
from . trap_card import TrapCard
from . spell_card import SpellCard
from . ritual_card import RitualCard
from . monster_card import MonsterCard
__all__ = [TrapCard, SpellCard, RitualCard, MonsterCard]
# The following line, for example, does not work from inside another package
# I'm trying to import the modules in cards from deck
from cards import TrapCard, MonsterCard, SpellCard, RitualCard
When I try to import packages from another folders I get this error message:
Traceback (most recent call last):
File "/root/git-repos/Yu-Gi-Oh/decks/deck.py", line 3, in
from cards import TrapCard, MonsterCard, SpellCard, RitualCard
ModuleNotFoundError: No module named 'cards'
When you call import *, python search the module from sys.path. You need to add your root dir into sys.path before call import stmt.
For your case, your root dir is /.
like:
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from cards import *
other way
Add file __init__.py into your root dir for make it become a module. Then change from cards import * to from .cards import *.
I have the following file structure
MainDirectory
| Subfolder1
| script1.py
| Subfolder2
| __init__.py
| script2.py
I want to import the the module script2.py in the script1.py.
With Python2.7 I was able to do it like this:
The __init__.py contains the code:
from script2 import ClassA
File script1.py contains the following import structure:
sys.path.insert(0, "../")
from SubFolder2 import ClassA
But when I run the same in Python3 then I get an
ImportError: No module named 'script2'
What would I have to change to get it to work with Python3?
This works as expected:
import sys
import os
sys.path.insert(0, '/'.join(os.path.dirname(os.path.abspath(__file__)).split('/')[0:-1])+'/Subfolder2')
import script2
You need to specify that this resource is in a parent directory...
from ..SubFolder2 import script2