简体   繁体   中英

Unable to import Modules in Python for Django Oscar

I have installed Django AND Oscar by running the commands in given order:

virtualenv eshop_env
eshop_env/Scripts/Activate

pip install django-oscar

django-admin.py startproject eshop


cd eshop
manage.py migrate

The settings.py file in eshop directory has these two lines a the top:

import os
from oscar.default import *

The os module is importe without any error. However, there is a red wavy line under from .

I am using Visual Studio Code. When I hover over the line, it says unable to import oscar.default . The same error appears on all my import statement involving django and oscar.

This also results in the follwing error in Command line after i run the migrate command:

ModuleNotFoundError: No module named 'oscar.default'

I tried running

pip install oscar.default
pip install oscar

but both of them show an error.

However, I was able to successfully run the pip install django-oscar command again. But, he error about the module does not change.

What am I doing wrong?

This is my project directory structure:

D:\Python Websites\Example\eshop\
D:\Python Websites\Example\eshop_env\

D:\Python Websites\Example\eshop\manage.py

D:\Python Websites\Example\eshop\eshop\settings.py, urls.py etc.

The import error occurs with all other modules as well:

from django.apps import apps
from django.urls import include, path  # > Django-2.0
from django.contrib import admin
from oscar.app import applications

Visual Studio Code shows a red way line for all of them with an error that starts like Unable to Import... .

I think you have installed django-oscar in the virtual environment and that is not the problem. I think you have to 'tell' the VS Code to use the specific virtualenv named eshop_env . Please check this link for details instructions on how to tell vscode which virtualenv to use: https://code.visualstudio.com/docs/python/environments . Good luck.

To check where Python is looking for your files, copy this code in settings.py

import os
import environ
import oscar

# To check where is looking Python

env = environ.Env()

from os.path import dirname, abspath
ROOT = dirname(abspath(__file__)).replace('\\', '/') + '/'

print ("self.__name__: " + __name__)
print ("self.__file__: " + __file__)
print ("ROOT: " + ROOT)


import django
print (django.__path__)
print (oscar.__path__)
print (environ.__path__)
...    

Then type 'python manage.py runserver' in console. I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM