Can anyone guide on resolving the error ? The manager class method will need capability to create instance of User Class but it is not finding the module
Below is the folder structure and code and other details. init.py file is empty
Internal folder
->main.py
->__init__.py
->User Folder
-> user.py
-> models.py
-> usermanager.py
-> __init__.py
ModuleNotFoundError: No module named 'models'
at <module> (/home/vmagent/app/internal/user/usermanager.py:4)
at <module> (/home/vmagent/app/main.py:2)
at import_app (/env/lib/python3.6/site-packages/gunicorn/util.py:352)
at load_wsgiapp (/env/lib/python3.6/site-
packages/gunicorn/app/wsgiapp.py:52)
at load (/env/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py:65)
at wsgi (/env/lib/python3.6/site-packages/gunicorn/app/base.py:67)
at load_wsgi (/env/lib/python3.6/site-
packages/gunicorn/workers/base.py:135)
at init_process (/env/lib/python3.6/site-
packages/gunicorn/workers/base.py:126)
at spawn_worker (/env/lib/python3.6/site-
packages/gunicorn/arbiter.py:578)
main.py code
from internal.account import user,usermanager
def registerBlueprint(app, blueprint, manager):
blueprint.manager = manager
app.register_blueprint(blueprint)
registerBlueprint(app, user.request, usermanager)
user.py code
request = Blueprint('flex.user', __name__)
@request.route('/v1/user', methods=['POST'])
def CreateAccount():
test = request.manager.UserManager()
return test.CreateUser()
usermanager.py
from models import User
class UserManager:
def __init__(self):
pass
def CreateUser(self):
logger.log_text("AccountManger")
data = json.dumps(dict(
email="[email protected]",
password="Test124"
))
user = User(data)
user.create()
responseObject = dict(status='success', message='Successfully
registered.')
return make_response(jsonify(responseObject)), 201
2 Answers 2
Python paths can get kind of tricky. I don't claim that this is the best approach, but it's what I hacked together on my lunch. Perhaps it can get you where you need to go.
When you run main.py, you're in the internal/ directory. You need to go up a level and then access the user/ directory. That's what happens with sys.path.append('../'). The module usermanager.py is in the user/ directory, so to import from user.py, we want to point to the current directory. Hence the period in from .user import User.
Here's the directory structure I used. I believe it is the same as what you have.
C:.
├───internal
│ main.py
│ __init__.py
│
└───user
│ models.py
│ user.py
│ usermanager.py
└───__init__.py
__init__.py
The __init__.py files are empty. Their presence indicates to Python that the directories are modules1.
main.py
import sys
sys.path.append('../')
from user import user
from user import usermanager
from user import models
my_user = user.User()
my_user.message()
my_user_manager = usermanager.UserManager()
my_user_manager.message()
my_user_manager.CreateUser()
my_model = models.Model()
my_model.message()
models.py
class Model():
def message(self):
print("This is a Model!")
user.py
class User():
def message(self):
print("This is a User!")
usermanager.py
from .user import User
class UserManager():
def message(self):
print("This is a UserManager!")
def CreateUser(self):
new_user = User()
print("I created the user: ", new_user)
Now, when I call main.py I get:
c:\project\internal>python main.py
This is a User!
This is a UserManager!
I created the user: <user.user.User object at 0x0000000002A61EB8>
This is a Model!
Everyone is talking to everyone else. Hopefully you can extrapolate that to your use case! If not, Chris Yeh has an article "The Definitive Guide to Python import Statements" you may find helpful.
1 See What is __init__.py for? to learn more about how that works.
Comments
There is no models module in the standard library, and you do not have a file named models.py listed here, so python cannot find it. If models.py is in some subdirectory, that subdirectory needs to be in your python path, and you need to be sure that they contain an __init__.py file. That is the meaning of the error you're encountering here.
3 Comments
__init__.py file in this folder (if so, what are its contents?), and where is main.py in relation to this folder?
usermanager.pynot indented in your actual code? If they're supposed to be methods ofUserManager, then they should be indented. I doubt this is what your problem is, but it jumped out at me so I figured I'd ask!