Django: ImportError: cannot import name 'GeoIP2' - django

I am trying to set up geoip2 for GeoDjango as per the instructions.
For some reason the wrapper isn't importing the function. It worked before I downloaded the databases and pointed to them in my settings, but for some reason now I can't load GeoIP2 (even when I comment out the line in settings.py). How should I troubleshoot this?
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import geoip2
>>>
>>> from django.contrib.gis.geoip2 import GeoIP2
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: cannot import name 'GeoIP2'
>>>
EDIT: I can see the GeoIP2 function correctly listed in the source file (which I haven't modified). What could possibly be preventing it from loading?
I am running Django 1.11.4
If I import django.contrib.gis.geoip2 this is it's __path__ property:
>>> geoip2.__path__
['C:\\Users\\Adam\\Envs\\otherlane\\lib\\site-packages\\django\\contrib\\gis\\geoip2']

I fixed by installing it through pip package geoip2==2.9.0
pip install geoip2==2.9.0

This module is Deprecated since version 1.9 in favor of django.contrib.gis.geoip2, which supports IPv6 and the GeoLite2 database format.
If you have a django < 1.9, use instead
from django.contrib.gis.geoip import GeoIP

just run the command pip install geoip2 then it will work fine...

Double check your GEOIP_PATH. And my I remind you that Windows requires back slashes not forward slashes.

I was also facing the same error and couldn't resolve it after multiple attempts.
Since it was working on one of my system, I found one difference, When you install geoip2 via pip it also installs maxminddb. The system on which it was working, maxminddb version was 1.5.4 and the one on which it was not working it was maxminddb==2.0.0
so finally I did
pip install maxminddb==1.5.4
and it worked

You can handle it this way
from geoip2 import geolite2

Related

ImportError: cannot import name cygrpc

I am trying to use Firebase Native mode on Google App Engine - Standard. My python language is Python 2.7. When I try to from google.cloud import firestore, I get an error ImportError: cannot import name cygrpc
I have deployed virtualenv described in the documentation here.
pip install virtualenv
virtualenv env
source env/bin/activate
My appengine_config.py is
from google.appengine.ext import vendor
import os.path
# Add any libraries installed in the "lib" folder.
vendor.add('lib')
vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
my_app.py includes
from google.appengine.ext.webapp import template
from google.appengine.ext import ndb
from google.appengine.api import mail
import os
from google.cloud import firestore
(/base/alloc/tmpfs/dynamic_runtimes/python27g/43d5822312de17fd/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py:269)
Traceback (most recent call last):
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/43d5822312de17fd/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/43d5822312de17fd/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 311, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/base/alloc/tmpfs/dynamic_runtimes/python27g/43d5822312de17fd/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/main.py", line 4, in <module>
from controllers import server, common, header
File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/controllers/server.py", line 10, in <module>
from google.cloud import firestore
File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/lib/google/cloud/firestore.py", line 18, in <module>
from google.cloud.firestore_v1 import __version__
File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/lib/google/cloud/firestore_v1/__init__.py", line 22, in <module>
from google.cloud.firestore_v1._helpers import GeoPoint
File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/lib/google/cloud/firestore_v1/_helpers.py", line 21, in <module>
import grpc
File "/base/data/home/apps/s~openbarn-prod/20190602t102855.418624175446659791/lib/grpc/__init__.py", line 23, in <module>
from grpc._cython import cygrpc as _cygrpc
ImportError: cannot import name cygrpc
The question I have - can you use Firestore Native mode on Google App Engine Standard using Python 2.7? I need GAE- Standard because we are using Google Endpoint that is not support on GAE-Flex.
The doc here says App Engine Client library integration is not supported on Python2.7 GAE Standard environment. But I am not trying App Engine Client library, I am trying App Engine Server library in GAE Standard Environment.
How do I solve for import error for cygrpc? The solution here, says -
python -m pip install grpcio --ignore-installed
Is this recommended?
It's 2020, and you can now use grpcio (without explicitly adding it yourself as it's a built-in library) w/Cloud Firestore natively on App Engine with Python 2.7. Three things to make this work:
Add google-cloud-firestore to your requirements.txt file and install with pip install -U -t lib -r requirements.txt.
Add these lines to the libraries section of your app.yaml file:
libraries:
- name: grpcio
version: 1.0.0
- name: setuptools
version: 36.6.0
Ensure these lines are in your appengine_config.py file:
import pkg_resources
from google.appengine.ext import vendor
# Set path to your libraries folder.
path = 'lib'
# Add libraries installed in the path folder.
vendor.add(path)
# Add libraries to pkg_resources working set to find the distribution.
pkg_resources.working_set.add_entry(path)
Of course, we do recommend you eventually migrate to Python 3 to take advantage of the greater flexibility the next generations (of Python and App Engine) provide, esp. the ability to tap into other GCP services. However, caveat such a port isn't without effort if your app is complex & deeply-dependent on App Engine's 1st gen built-in services. Most of the suggestions above are derived from this section of the migration docs.
Once you're on Python 3, everything related to 3P libraries on App Engine becomes much easier, as I demonstrate in this other SO answer.
A while ago GRPC wasn't supported on GAE standard, see GRPC and types import error in App Engine Datastore. I didn't try since, but I don't see newer activity on issue 149.
Also the cython reference in the traceback suggests that it may include compiled code, which would violate the pure python standard environment sandbox restrictions that applies to the code you deploy.
Make sure that your installed python version bit(32/64) and your system bit(32/64) are matched.
If they are not matched please follow the solution mentioned at https://azurelessons.com/cannot-import-name-cygrpc/

Odoo custom module with external Python library

I created an Odoo Module in Python using the Python library ujson.
I installed this library on my development server manually with pip install ujson.
Now I want to install the Module on my live server. Can I somehow tell the Odoo Module to install the ujson library when it is installed? So I just have to add the Module to my addons path and install it via the Odoo Web Interface?
Another reason to have this automated would be if I like to share my custom module, so others don't have to install the library manually on their server.
Any suggestions how to configure my Module that way? Or should I just include the library's directory in my module?
You should try-except the import to handle problems on odoo server start:
try:
from external_dependency import ClassA
except ImportError:
pass
And for other users of your module, extend the external_dependencies in your module manifest (v9 and less: __openerp__.py; v10+: __manifest__.py), which will prompt a warning on installation:
"external_dependencies": {
'python': ['external_dependency']
},
Big thanks goes to Ivan and his Blog
Thank you for your help, #Walid Mashal and #CZoellner, you both pointed me to the right direction.
I solved this task now with the following code added to the __init__.py of my module:
import pip
try:
import ujson
except ImportError:
print('\n There was no such module named -ujson- installed')
print('xxxxxxxxxxxxxxxx installing ujson xxxxxxxxxxxxxx')
pip.main(['install', 'ujson'])
In python file using the following command, you can install it (it works for odoo only). Eg: Here I am going to install xlsxwriter
try:
import xlsxwriter
except:
os.system("pip install xlsxwriter")
import xlsxwriter
The following is the code that is used in odoo base module report in base addons inside report.py (odoo_root_folder/addons/report/models/report.py) to install wkhtmltopdf.
from openerp.tools.misc import find_in_path
import subprocess
def _get_wkhtmltopdf_bin():
return find_in_path('wkhtmltopdf')
try:
process = subprocess.Popen([_get_wkhtmltopdf_bin(), '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except (OSError, IOError):
_logger.info('You need Wkhtmltopdf to print a pdf version of the reports.')
basically you need to find some python code that will run the library and install it and include that code in one of you .py files, and that should do it.

Python can't locate modules which I have created

I am new to programming and have recently installed the Enthought Canopy distribution and can't seem to import certain modules.
Python 2.7 MacOSX
Numpy works when I import it, however other modules which I have created or downloaded as a simple module.py file return this error message:
import numfun1
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-297-cb46e477a372> in <module>()
----> 1 import numfun1
ImportError: No module named numfun1
Could it have something to do with where those modules are saved? If so, how do I point python in their direction? or where should I put those modules so that Python sees them.
Thank you in advanced for your suggestions.
Information about the module search path is included in the official Python tutorial: http://docs.python.org/2/tutorial/modules.html#the-module-search-path.
A lot of python libraries come with a setup.py script that will automatically install them into locations that are on the search path.
The installation process can be even more automated by using a Python package manager like pip.
If you create a module you must put it where your script is.

Adding periodic tasks dynamically from flask app

I have a flask web app deployed in heroku. I need to schedule a background task to be scheduled at a specific time. I have tried using the apscheduler module. While it allows to define periodic tasks easily adding them from your application at runtime is what I am looking for.
I tried sharing the same jobstores in apscheduler
import time
from apscheduler.scheduler import Scheduler
from apscheduler.jobstores.shelve_store import ShelveJobStore
sched = Scheduler()
sched.add_jobstore(ShelveJobStore('jobstore.db'), 'shelve')
sched.start()
And from terminal I tried this,
Python 2.7.5 (default, May 12 2013, 12:00:47)
[GCC 4.8.0 20130502 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from apscheduler.scheduler import Scheduler
>>> sc = Scheduler()
>>> sc.add_jobstore('jobstore.db', 'shelve')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/dhananjay/git/blast/venv/lib/python2.7/site-packages/apscheduler/scheduler.py", line 168, in add_jobstore
jobstore.load_jobs()
AttributeError: 'str' object has no attribute 'load_jobs'
I have come across this question, while looking for a celery based approach. It talks about the same problem from a django perspective but I can't get it to work with my app (I am completely oblivious to django)
When you tried running it from the terminal, you gave add_jobstore a string as the first parameter, instead of a job store. It expects a job store as the first parameter, see the documentation for more info.
As for scheduling background tasks in Heroku, I would recommend reading the Worker Dynos, Background Jobs and Queueing article on the matter.

Django 1.4 Development Environment with zc.buildout

I am trying to create a dev environment for the Django 1.4 project using the following guide:
http://www.stereoplex.com/blog/a-django-development-environment-with-zc-buildout
virtualenv part of the guide runs ok with the following output:
virtualenv project
New python executable in project\Scripts\python.exe
Installing setuptools................done.
Installing pip...................done.
After that I am able to activate dev environment. Now I create directory named Source, download the bootstrap.py to it and create a buildout.cfg with the following content:
[buildout]
parts =
And run bootstrap.py for the following result:
Creating directory 'C:\\Dropbox\\XYZ\\project\\Source\\bin'.
Creating directory 'C:\\Dropbox\\XYZ\\project\\Source\\parts'.
Creating directory 'C:\\Dropbox\\XYZ\\project\\Source\\eggs'.
Creating directory 'C:\\Dropbox\\XYZ\\project\\Source\\develop-eggs'.
Generated script 'C:\\Dropbox\\XYZ\\project\\Source\\bin\\buildout'.
Here comes the problem part - Installing Django I configure the buildout.cfg to the following and run bin\buildout created by bootstrap:
[buildout]
parts = django
[django]
recipe = djangorecipe
version = 1.4
After running bin\buildout i get the following error:
(project) C:\Dropbox\XYZ\project\Source>bin\buildout.exe
Traceback (most recent call last):
File "C:\Dropbox\XYZ\project\Source\bin\buildout-script.py", line 15, in <module> import site # imports custom buildout-generated site.py
File "C:\Dropbox\XYZ\project\Source\parts\buildout\site.py", line 601, in <module> main()
File "C:\Dropbox\XYZ\project\Source\parts\buildout\site.py", line 584, in main known_paths = addsitepackages(known_paths)
File "C:\Dropbox\XYZ\project\Source\parts\buildout\site.py", line 328, in addsitepackages import pkg_resources
ImportError: No module named pkg_resources
Although if I run python directly in project environment I can import pkg_resources with no error:
(project) C:\Dropbox\XYZ\project\Source>python
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg_resources
>>> pkg_resources
<module 'pkg_resources' from 'C:\Dropbox\XYZ\project\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_resources.py'>
I am completely struck here. Any suggestions?
I don't have the definitive answer, but here are some brainstorm thoughts:
Why the virtualenv? Buildout itself provides isolation, so no virtualenv is needed. Could you re-try with just a bootstrap.py and your buildout.cfg? So just run bootstrap.py with your system python?
Do you have buildout installed globally, perhaps? They can interfere.
The latest 1.5.2 buildout has some problems with site.py files in some situations, which is a possible reason for it failing inside a virtualenv. Could you try the special 1.4.4 bootstrap mentioned in http://pypi.python.org/pypi/zc.buildout/1.5.2#system-python-and-zc-buildout-1-5 ?
bin/buildout -vvv gives you much more debugging info.
An additional comment: the version setting in djangorecipe is deprecated in the latest versions of djangorecipe. You can remove it. If you want to pin Django you have to pin it in your buildout's [version] list.
The guide you are following is a little bit obsolete. It will fail when processing the [django] part, specifically in the version variable. You must specify the versions in the new way, which is showed in the djangorecipe page. This is:
[buildout]
parts = django
versions = versions
[versions]
django = 1.4