ModuleNotFoundError: No module named 'app.lang'; 'app' is not a package - python

I have this file structure in my python project
|__src
|__main.py
|__gen.py
|__app
|__ __init__.py
|__ app.py
|__ lang.py
Intention
I want to use the Language class from sibling module lang.
So I tried with this import statement in app.py:
from app.lang import Language
Issue
But when I run app.py I get a ModuleNotFoundError error saying 'app' is not a package:
Which doesn't make sense since app has __init__.py.
How can I solve this?

Because both app.py and lang.py are in the same directory try to import like this :
from .lang import Language
or you can use from app.lang import Language from another file located outside app folder

Related

ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package

I have a Django project with the following structure:
project
apps
news
__init__.py
models.py
hose
tasks.py
Within tasks.py, I have this import
from apps.news.models import Company
When I try to run tasks.py, I get the following error:
ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package
How do I fix this error? I don't understand why this import does not work.
Reference: https://docs.python.org/2/tutorial/modules.html#packages
You need to add an empty __init__.py (4 underscores in total) file in the apps folder for it to be recognized by Python as a package.
In case anybody else is still struggling with this. I had the __init__.py in the app folder but it still didn't work. Running the following command in app parent folder worked for me.
export PYTHONPATH=$PWD

python 3 import module not found error

Im trying to import a module from a different sub directory in Python 3 and I'm having trouble getting the script running. The current file structure I have is:
/lets_import
__init__.py
/app
__init__.py
main.py
server.py
/tests
__init__.py
tests_main.py
tests_main.py imports a function from server.py like this:
from app.server import add
(add is the function being imported from server.py)
init files are empty. They are there so that each sub dir is seen as a package.
When I run tests_main.py I get the following error
File "tests/tests_main.py", line 2, in <module>
from myapp.server import add
ImportError: No module named 'myapp'
I tried running it from the root level folder like this python test/tests_main.py but still got the same error.
Is there a way to do this without having to manipulate PYTHONPATH?

Import method from __init__.py

Having some issues with importing modules in python. This is my folder structure
my_app/
app.py
__init__.py (I want to import a function from this file)
folder1/
__init.py
method1.py
folder2/
__init__.py
method.py
In my root __init__.py I have this function
def want_to_be_run_elsewhere():
pass
In my app.py, I want to import this function and run it when I start my application, but I'm unsure how to do it.
from my_app import want_to_be_run_elsewhere
This throws a no module named my_app
From what I can tell, I have all the necessary __init__.py files, so maybe it could be sys.path related?
I've read some similar threads on here but I haven't been able to solve this.
Usually you would do the import like
from . import want_to_be_run_elsewhere
This doesn't work here, because you are calling app.py. If you import my_app.app, it is part of the module. If you call it it is not. Importing from the module it is in using . will then not work.
You could either move app.py outside of my_app, removing it from the module and making imports work again.
Or you can use
from __init__ import want_to_be_run_elsewhere
in app.py
I believe
from my_app import want_to_be_run_elsewhere
will only work if you have actually pip install -e my_app/. Then it should work too.

import python class into another folder in a package

I'm trying to import a python class from another folder of my python package but I this error "No module named models".
It's a Flask project.
-app
|-run.py
|-myapp
|- libs
|- updateto
|- __init__.py (empty)
|- connection.py
|- removeto
|- __init__.py (empty)
|- static
|- templates
|- __init__.py (NOT empty)
|- routes.py
|- models.py
My init.py:
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///maindb.db'
from models import db
db.init_app(app)
import myapp.routes
In my models.py I've a class named: application
And in connection.py I want import my class "application".
I've try with "from models import application" but I've this error "no modules named models".
I think it's possible to reach this goal with the modification of the init.py in the "updateto" folder but I'm no sure because I don't clearly understand the functioning of the init.py file...
Thanks
EDIT:
Which is weird, it's if under connection.py I add "import myapp" and "print(help(myapp))", I've this output:
Help on package myapp:
NAME
myapp
FILE
c:\app\myapp\__init__.py
PACKAGE CONTENTS
libs (package)
models
routes
DATA
app = <Flask 'myapp'>
But if I try "from myapp import models" I've this error "ImportError: cannot import name models"
EDIT2:
If I try to add in connection.py "from myapp import app", it works but not if I try to import models... I'm very surprised. I've try to add in myapp/init.py:
__all__ = ["models", "routes]
But same error...
EDIT3:
hummmm I've move all my modules under the libs folder into myapp and when I try to put in connection.py "from model import application" I've always the same error "ImportError: cannot import name application"
Other test in connection.py:
import models
print(models)
Output:
<module 'myapp.models' from 'C:\app\myapp\models.pyc'>
I can import my module named models but I can't import the class under models.py
EDIT4:
Ok, I think I found the problem. It's my ini.py in myapp, because if I comment this file content, the connection.py can now import my class application in models. I run the file run.py which uses routes.py and it's him that calls operation.py
With some tests, I've find the lines who create my error:
from models import db
db.init_app(app)
import myapp.routes
But why????
I don't know about Flask specifically, but in Python you should generally include a __init__.py in every folder and subfolder for them to be recognised as modules and/or packages.
In the diagram you provide, there is no __init__.py in the app folder. Try putting one and re-post if that resolves your problem.
If it doesn't work, try:
import os
print(os.getcwd())
If the current working directory isn't the app directory, you'll have to work around this. One way would be:
import sys
sys.path.append('/path/to/app')
import myapp.models
You can always see what directories are in your search path for modules by checking sys.path.
Update: Sorry I just realized that maybe you' re trying to run connections.py as a script. That would change you working directory to that of connections.py. Try importing connections.py from your main script with import models inside connections.py and see if you get an error.
Update (in reply to Edit 4): (I don't have enough reputation points to comment yet, so I reply here)
from models import db
db.init_app(app)
import myapp.routes
The problem in this piece of code is the import myapp.routes. Change that to import routes and it should work. As a rule of thumb, your main script's directory is your root directory. You can import any module located in your root directory from inside any other module (even modules inside a sub-folder!) with a simple import module.

ImportError: no mudule named ini

I have a directory structure like
akom
DB
DBinit.py
__init__.py
server.py
Configuration(dir)
urls.py
settings.py
manage.py
ini.py
__init__.py
I have set the PYTHONPATH in my .bashrc as export PYTHONPATH=$PYTHONPATH:/home/dir1/dir2/
Inside dir2 akom reside.
I am importing the ini.py on server.py like import ini but i am getting the error that
ImportError: No module named ini
Please tell me where i am going wrong .?
Since akom is a package itself, you need to reference modules inside that package by the package name first - e.g. by using import akom.ini instead of import ini.
Update: Reading above, it's somewhat unclear if ini.py is in the Configuration directory - if it is you will need to do import akom.Configuration.ini.

Categories