Unable to import Modules in Python for Django Oscar - python

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.

Related

Django - ModuleNotFoundError: No module named 'csvexport'

So I'm working on adding an admin function to a Django project so that I can export the model instances to csv. It is all working fine locally whilst using Docker. But when deploying I get this internal server error:
It states that the package is not installed, but when accessing the Django shell (poetry run python3 manage.py shell) and importing the package (from csvexport.actions import csvexport) everything works fine. I'm stuck for quite some time now on this and cant figure out what is going wrong. Anyone had some kind of problem like this with poetry package managing?
settings.py:
INSTALLED_APPS = [
...
'csvexport',
]
model:
from csvexport.actions import csvexport
from django_summernote.admin import SummernoteModelAdmin
class FundAdmin(SummernoteModelAdmin):
...
actions = [csvexport]
manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Looking at the documentation, seems you followed the steps apart from pip install django-admin-csvexport, so my only assumption is that you have not installed the requirement. Can you try adding this to your requirements?

Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? --> WSGI.PY file

When I try to execute py manage.py runserver I had the following message:
raise ImportError(
Import Error: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
)
I found a lot of possible solutions on the internet. I suggest first try the following:
Go to the wsgi.py file, inside MySite and go to this line:
from django.core.wsgi import get_wsgi_application
Verify that Django is imported. I use Pycharm, the IDE allowed me to import directly from there.
After this I was able to run manage.py
Try to make it in a virtual environment, it will work:
First, in cmd type pip install virtualenvwrapper
Make a folder in c drive which will consist all of your django projects like C:/Django
Then go to cmd and type mkvirtualenv [name you want to give] and then close the cmd
Open it again and then navigate to the folder C:/Django then when on that folder type workon [the name which you gave]
Finally, install django by the pip command and then runserver

How to import two files from different directories?

Try to run this file:
#!flask/bin/python
from flask import Flask, jsonify
#!/usr/bin/python
import psycopg2
And this return:
ImportError: No module named psycopg2
I have file with only psycopg2 and it is work. And I have another file with only flask and it is work too. But when I try to run file with both imports it crush.
How to fix it (include flask/bin/python and /usr/bin/python, that flask and psycopg2 work toogether)?
#! PATH is shebang and Unix/Linux use it for path for executor. (/path/to/script.py insted of python /path/to/script.py)
However, it might help to extend the path in which to look for packages.
Run for your extra enviroment (python bin) this:
# Example:
python -c "import PACKAGE; print(PACKAGE.__path__)"
# Psycopg2
python -c "import psycopg2; print(psycopg2.__path__)"
Output:
['/path/to/some/python/psycopg2']
(My output: /home/usr/miniconda3/envs/free/lib/python3.6/site-packages/psycopg2)
#!flask/bin/python
from flask import Flask, jsonify
import sys
sys.path.insert(0, '/path/to/some/python') # Without package name
import psycopg2
If you add a path with a package name, you must then be one level down, but beware, __init__.py files can edit paths through the "namespace" so it's not ideal. But you only work with the package itself. Without it, you work with packages that are available in that directory.
You have probably two different python installations running.
One in [some path]/flask/bin/python and one in /usr/bin/python. The modules are installed in different environments so you can not use them together.
To fix this install flask module in your 'main' python installation or install psycopg2 in your flask environment.
You need to install a package module.
In terminal execute:
pip install psycopg2
more information in PyPi - PSYCOPG2

Cookiecutter created directory giving me issues running development server and python shell

I created a django project using cookiecutter as reccomended by Two scoops of Django 1.8. It's called icecreamratings_project
I use the git cmd prompt and use
'cd icecreamratings_project'.
When i want to use the built-in python interpreter by using
python manage.py shell it gives me the following error.
File "C:\Users\Armando\Desktop\icecreamratings_project\config\settings\common.py", line 13, in
import environ
ImportError: No module named 'environ'
I looked into the directory and the following code is there:
from __future__ import absolute_import, unicode_literals
from sys import path
import environ
ROOT_DIR = environ.Path(__file__) - 3 # (/a/b/myfile.py - 3 = /)
APPS_DIR = ROOT_DIR.path('twoscoops_project')
env = environ.Env()
No module named environ exists, but I'm assuming environ is in reference to the virtual environment. Im not familiar with the cookiecutter documentation or how it creates django templates, but i created a virtual environment named environ.
The message i got after that is that there is no Path in environ. Can someone help?
The environ module can be found in django-environ.
django-environ is a requirement of cookiecutter-django's requirements/base.txt.
base.txt is a requirement of cookiecutter-django's requirements/local.txt.
It seems you'll install environ and other needed modules by completing the following steps from cookiecutter-django's README.rst:
Getting up and running
The steps below will get you up and running with a local development
environment. We assume you have the following installed:
pip
virtualenv
PostgreSQL
First make sure to create and activate a virtualenv, then open a
terminal at the project root and install the requirements for local
development:
$ pip install -r requirements/local.txt
Source: https://github.com/pydanny/cookiecutter-django#getting-up-and-running

Problem with Python modules

I'm uploading my first Django app to my Dreamhost server. My app uses xlwt package and since I can't install it in the default location ( /usr/lib/python2.3/site-packages/xlwt ), I installed it on another location by:
python setup.py install --home=$HOME
Then xlwt is installed here:
/home/myuser/lib/python/xlwt/
After that, I add this folder to de env var PYTHONPATH
export PYTHONPATH=$PYTHONPATH:/home/myuser/lib/python
... And in a python promt I can do this (without problems)
import xlwt
... But if I do the same thing in my app code, I have the follow error:
Could not import ISI.restaurante.views. Error was: No module named xlwt
[where ISI.restaurante.views is my code where I do the import]
Could u help me? Thanks!
PYTHONPATH may only be set when you run from the shell, you can set path programatically from python using
import sys
sys.path.append('/home/myuser/lib/python')

Categories