Forums

Maybe about the sys.path

Hi, There is a problem. I've installed django-cart into my project. And in views.py, I import it as:

from cart.cart import Cart

But it keeps showing that in the cart.py, ModuleNotFoundError, which is related to this line:

import models

There is a models.py located at the same folder with cart.py, the folder is

"/home/adrop/venv/lib/python3.6/site-packages/cart".

And I also try to add this path to the sys.path, which makes my sys.path looks like this:

['', '/home/adrop/venv/lib/python36.zip', '/home/adrop/venv/lib/python3.6', '/home/adrop/venv/lib/python3.6/lib-dynload', '/usr/lib/pyth on3.6', '/home/adrop/venv/lib/python3.6/site-packages', '/home/adrop/venv/lib/python3.6/site-packages/cart']

And it refused to find models.py as well.

So, is there anything I missed?

Can we take a look at your files? We can see them from our admin interface, but we always ask for permission first.

Yes, please

Hi there,

it looks like your most recent error is:

RuntimeError: Model class mycart.models.Cart doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

that's a different error? Perhaps you need to add something to INSTALLED_APPS in settings.py?

I've copy the models.py to another folder, and temporary remove the error and come up with the new error.

I'll change it back and please take a look again.

Thank you.

At line 15 in mainsite/views.py, you have

from cart.cart import Cart

Should that be

from mycart.models import Cart

?

Or are you trying to use a third-party module called "cart" that you've installed into your virtualenv? If so, it looks like that module is broken...

yes, I've trying to use a third-party module called "cart" which I've already installed.

That's an old module, I found out these few days. It must have some problems.

But what's confusing, is why cart.py and models.py located at the same folder, yet cart.py couldn't include models.py. Is it normal?

I create a folder "mycart" under my project, and copy models.py to this folder.

And then in /home/adrop/venv/lib/python3.6/site-packages/cart/cart.py

I import this models.py as

import mycart.models

It can be included. But just can't import it from the same folder.

If the folder is not on the Python path, then you will not be able to import directly from it.

In my first post, I listed the sys.path, and the folder is listed in it. Is sys.path Python path? I'm a little confused.

Have a look at http://help.pythonanywhere.com/pages/DebuggingImportError/. It has a really good description of how Python tries to find modules that you import.