Django 1.4 Development Environment with zc.buildout - django

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

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/

Django: ImportError: cannot import name 'GeoIP2'

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

uWSGI - ImportError: No module named google.oauth2.credentials

I am developing a Flask app that runs using uwsgi. I'm running into the following error ImportError: No module named google.oauth2.credentials.
When I run the Flask app using Flask's builtin dev server, the application runs without issues.
The problem seems to stem from how the google-auth module is installed. There's no __init__.py file in the google namespace folder in site-packages. The full path being /usr/local/lib/python2.7/site-packages/google.
I'm aware that python3.3 has implicit namespace packaging via PEP-420.
What I'm confused about is how the implicit namespace packaging works in python2.7. Does the installation process via pip do something special?
I'm also confused about what is different when running python2.7 via uwsgi.
Relevant versions
python 2.7.14
pip 9.0.3
uwsg 2.0.13
google-auth 1.4.1
Installed via pip (not using virtualenv)
https://github.com/GoogleCloudPlatform/google-auth-library-python
I've also included the following barebones test to reproduce the error.
test.ini
[uwsgi]
socket = 0.0.0.0:5001
plugins-dir = /usr/lib/uwsgi/
plugins = python
protocol = uwsgi
pythonpath = /usr/local/lib/python2.7/site-packages
callable = app
max-requests = 1000
master = True
lazy-apps = True
processes = 1
test.py
import google.oauth2.credentials
working command
python test.py
failing command
uwsgi --ini test.ini --wsgi-file test.py
failing result
When I run the above command, the program will fail due to the following ImportError.
Traceback (most recent call last):
File "test.py", line 1, in <module>
import google.oauth2.credentials
ImportError: No module named google.oauth2.credentials
Current workaround
My current workaround is to manually add an __init__.py file with the following (which is what is basically what is found in the google-auth repository, but for some reason is removed when installing via pip):
try:
import pkg_resources
pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
Workaround 2
Another way I got the import working was by doing this:
import site
site.addsitedir('/usr/local/lib/python2.7/site-packages')
import google.oauth2.credentials
This seems to imply that uwsgi is not fully initialization python at least on initial start up.
I got the hint to try site.addistedir from this answer: https://stackoverflow.com/a/15209116/177646
Turns out the problem was a uWSGI configuration error on my part. Since I'm not using virtualenv, I needed to set PYTHONHOME to the correct value, which is /usr/local in my case. In the uwsgi configuration, this can be specified via the home parameter. Once this is set, then python seems to work normally.
Once the PYTHONHOME is correctly set, this allows the google_auth-1.4.1-py3.6-nspkg.pth file to be found.
This also explains why I had to always import /usr/local/lib/python2.7/site-packages in my PYTHONPATH. Once the PYTHONHOME is set, then I no longer needed to add site-packages to my PYTHONPATH.
Example:
[uwsgi]
socket = 0.0.0.0:5001
plugins-dir = /usr/lib/uwsgi/
plugins = python
protocol = uwsgi
home = /usr/local
callable = app
max-requests = 1000
master = True
lazy-apps = True
processes = 1

gcloud command suddenly doesn't work at all

All gcloud commands suddenly give this error:
ERROR: gcloud failed to load: No module named google.appengine.ext
This usually indicates corruption in your gcloud installation or
problems with your Python interpreter.
Please verify that the following is the path to a working Python 2.7
executable:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
If it is not, please set the CLOUDSDK_PYTHON environment variable to
point to a working Python 2.7 executable.
If you are still experiencing problems, please reinstall the Cloud SDK
using the instructions here:
https://cloud.google.com/sdk/
Here are some relevant details and things that I've tried:
Mac 10.11.6
Python 2.7.8 at /Library/Frameworks/Python.framework/Versions/2.7/bin/python
Python otherwise works fine
I completely removed google-cloud-sdk and redownloaded from https://cloud.google.com/sdk/
Tried rebooting
Tried "export CLOUDSDK_PYTHON=/Library/Frameworks/Python.framework/Versions/2.7/bin/python" but it didn't help. Also added this line to my .bash_profile
Anyone else having this problem or can suggest a fix?
I have a fix below, but I'll happily award the answer to anyone who can explain what is going on.
I had the same issue . I solved it by using the default python2.7 in Ubuntu
Type this in termimal
export CLOUDSDK_PYTHON=/usr/bin/python
Not sure if this will work the same on Windows
I followed the answer here, worked for me
https://stackoverflow.com/a/59816812/2210667
brew switch openssl 1.0.2q (or whatever latest version you have from ls /usr/local/Cellar/openssl)
I figured out what was causing the problem, though I don't understand what is going on... When reinstalling gcloud I skipped the install script (./google-cloud-sdk/install.sh) since it is an optional step and I already had the needed lines in my bash profile.
When running this script, however, I got this error:
$ ./google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!
Traceback (most recent call last):
File "/Users/.../google-cloud-sdk/bin/bootstrapping/install.py", line 8, in <module>
import bootstrapping
File "/Users/.../google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 17, in <module>
import oauth2client.contrib.gce as gce
File "/Users/.../google-cloud-sdk/lib/third_party/oauth2client/contrib/gce.py", line 23, in <module>
import httplib2
File "/Users/.../google-cloud-sdk/lib/third_party/httplib2/__init__.py", line 29, in <module>
import email
File "/Users/.../GIT/GAE_Project/email.py", line 1, in <module>
ImportError: No module named google.appengine.ext
A file I recently created in my GAE project named email.py broke gcloud!?! Renaming this file fixed gcloud.
This is my PYTHONPATH:
/Users/.../google-cloud-sdk/platform/google_appengine:/Users/.../GIT/GAE_Project
So even though the Google cloud libraries come before my GAE project in my PYTHONPATH, a file in my GAE project broke gcloud.

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.