using python 3.4, django 1.9.7 ,django_ajax 0.2.0 ;
and test in python 2.7 too ;
Here is my code :
from django_ajax.decorators import ajax
from models import Product
from cart.cart import Cart
#ajax
def ajax_add_to_cart(request):
if 'product_id' in request.GET and request.GET['product_id']:
product_id = request.GET['product_id']
product = Product.objects.get(id=product_id)
cart = Cart(request)
cart.add(product, product.price, quantity=1)
items_in_cart = cart.itemCount()
return {'items_in_cart': items_in_cart}
I get this error :
from django_ajax.decorators import ajax
ImportError: cannot import name 'ajax'
Tnx for help
You've apparently installed the wrong package due to name similarity with another package. That usually happens.
You've installed django_ajax 0.2.0 while you intend to use djangoajax which has the ajax decorator. The former does not have or use decorators.
Remove django_ajax with:
pip uninstall django_ajax
Then install djangoajax which is usually imported as django_ajax and would be added as django_ajax to INSTALLED_APPS:
pip install djangoajax
Your import would no longer raise an error:
>>> from django_ajax.decorators import ajax
>>>
Related
Is the H2OModelSelectionEstimator deprecated? When I run the code
from h2o.estimators import H2OModelSelectionEstimator
I get the message: ImportError: cannot import name 'H2OModelSelectionEstimator' from 'h2o.estimators'
Try this instead:
from h2o.estimators.model_selection import H2OModelSelectionEstimator
If you can't import it, then you probably don't have the latest version of H2O, so you should download it. ModelSelection was just released in 3.36.0.1.
I'm implementing a graphql solution using python, graphene and django and I'm getting the following import error:
Result: Failure Exception: ImportError: cannot import name 'force_text' from 'django.utils.encoding'
"/home/site/wwwroot/.python_packages/lib/site-packages/graphene_django/utils/utils.py", line 6, in <module> from django.utils.encoding import force_text
I'm not sure about the versions and whether I need to import an additional module.
My requirements.txt is like:
graphene>=2.1,<3
graphene-django>=2.1,<3
graphql-core>=2.1,<3
graphql-relay==2.0.1
django-filter>=2
Has someone had a similar problem and can look at the versions that I use?
Thanks
in django 4.0 we dont have force_text
https://docs.djangoproject.com/en/4.0/ref/utils/#module-django.utils.encoding
instead change force_text to force_str
linux:
YOUR_VENV/lib/PYTHON_VERSION/site-packages/graphene_django/utils/utils.py
windows:
YOUR_VENV/lib/site-packages/graphene_django/utils/utils.py
from django.utils.encoding import force_text
to
from django.utils.encoding import force_str
and
def _camelize_django_str(s):
if isinstance(s, Promise):
s = force_text(s)
return to_camel_case(s) if isinstance(s, six.string_types) else s
to
def _camelize_django_str(s):
if isinstance(s, Promise):
s = force_str(s)
return to_camel_case(s) if isinstance(s, six.string_types) else s
Based on answer given by #Osman.
The problem seems to be occuring with Django-4. Till the PR gets merged, probably this monkeypatching might work (not tested in prod):
import django
from django.utils.encoding import force_str
django.utils.encoding.force_text = force_str
Put this in entryfile. I kept it in settings.py for time being.
In Django version 4> just paste this snippet to your settinsg.py. Preferably on the top
import django
from django.utils.encoding import force_str
django.utils.encoding.force_text = force_str
"force_text" is removed from Django 4.0:
Features removed in 4.0
But graphene_django still uses "force_text" in utils.py.
utils.py:
from django.utils.encoding import force_text # Line 6
s = force_text(s) # Line 29
So you need to replace "force_text" with "force_str" as shown below:
utils.py:
from django.utils.encoding import force_str# Line 6
s = force_str(s) # Line 29
These are the paths to utils.py for Linux and Windows:
Linux:
<your_venv>/lib/<python_version>/site-packages/graphene_django/utils/utils.py
Windows:
<your_venv>/lib/site-packages/graphene_django/utils/utils.py
adding the following to the requirements.txt solved it:
django<=3
You can install graphene-django version 3.0.0b7
run the following command in your terminal:
pip install graphene-django==3.0.0b7
It's beta version, but i don't know why graphene-django 2.15 does'nt work, when patch note said they fixed issue since 2.8.1 version.
Thank's to #Behoston from this Github issue for this solution
I am a newcomer to Django python programming. Now I am working on the server side. I want to use google maps API, my view.py is like this:
from django.shortcuts import render
from django.shortcuts import HttpResponse
from googlemaps import *
# Create your views
gmaps = googlemaps.Client(key='A')
def index(request):
if request.method=="GET":
geocode_result = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
return geocode_result
Also, I have already installed 'googlemaps' using pip. And when I import it in IDE, nothing goes wrong. But when I want to start the server to test the code, the server won't run and tells me ModuleNotFoundError: No module named 'googlemaps', I am confused that I have already downloaded it and added it in settings.py, importing in IDE also seems fine. But where I did wrong makes it fail to start the server?
Change from googlemaps import * to import googlemaps
What from googlemaps import * does is that it imports all the contents of the googlemaps module. import googlemaps imports the whole googlemaps module as a whole.
I got it working two different ways (assuming valid API key). Either:
from googlemaps import Client
gmaps = Client(key='A')
or:
from googlemaps import *
gmaps = Client(key='A')
Do either of these work for you?
If you're still having problems, there's a good chance it's related to your virtualenv (if using). Try running:
pip freeze
from command line and searching for the requisite library.
If you don't have an API key, you may of course want to consider using geopy. Last I checked, didn't require API key.
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("175 5th Avenue NYC")
Using rpy2, I want to check if a given package is installed. If it is, I import it. If not, I install it first.
How do I check if it's installed?
from rpy2 import *
if not *my package is installed*:
rpy2.interactive as r
r.importr("utils")
package_name = "my_package"
r.packages.utils.install_packages(package_name)
myPackage = importr("my_package")
Here is a function that'd do it on the Python side
(note the contriburl, that should be set to a CRAN mirror, and that the case where installing the library is failing is not handled).
from rpy2.rinterface import RRuntimeError
from rpy2.robjects.packages import importr
utils = importr('utils')
def importr_tryhard(packname, contriburl):
try:
rpack = importr(packname)
except RRuntimeError:
utils.install_packages(packname, contriburl = contriburl)
rpack = importr(packname)
return rpack
You can use the following function I got from #SaschaEpskamp's answer to another SO post:
pkgTest <- function(x)
{
if (!require(x,character.only = TRUE))
{
install.packages(x,dep=TRUE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
}
And use this instead to load your packages:
r.source("file_with_pkgTest.r")
r.pkgTest("utils")
In general, I would recommend not try to write much R code inside Python. Just create a few high-level R functions which do what you need, and use those as a minimal interface between R and Python.
import sys,subprocess
your_package = 'nltk'
package_names = subprocess.Popen([pip freeze],
stdout=subprocess.PIPE).communicate()[0]
pakage = package_names.split('\n')
for package in packages:
if package ==your_package:
print 'true'
I can't believe I'm not able to find this, but this is quite simple:
from google.appengine.ext.webapp import template
try :
content = template.render(...)
except TemplateDoesNotExist:
content = ...
Then I don't know how to import TemplateDoesNotExist! Can anyone tell me its module path? Thanks!
TemplateDoesNotExist is defined in django.template However from what you are doing it appears your going down a deprecated path under Python 2.7 . If you have a look at the template.py you just imported from google.appengine.ext.webapp import template you will see the following deprecation warning.
_PYTHON27_DEPRECATION = (
'google.appengine.ext.webapp.template is deprecated. Please use another '
'templating system such as django.template or jinja2.')
You haven't said if your using python 2.5 or python 2.7 . If your starting a new project the recommendation from google is to use 2.7.
If you're using python 2.7 and importing template as
from google.appengine.ext.webapp import template
Then you can import the exception as
from google.appengine._internal.django.template import TemplateDoesNotExist
and then
try:
template.render(...)
except TemplateDoesNotExist:
# do something useful