问题
I'm facing an issue with the init of ctypes
if int(_os.uname()[2].split('.')[0]) < 8:
ValueError: invalid literal for int() with base 10: '
My app is written in Python 2.7 and based on Django, running locally using App Engine SDK and virtualenv. It's been in production for a long time and is running successfully locally on other machines. Meaning this is a local issue for my machine. I'm on Mac OSX 10.15.3 Catalina.
The issues arises when I'm loading any page of my app, after starting the app engine local development server. The App Engine server is starting successfully (I can access the app engine admin webpag), but the above exception is being raised (see callstack below), as it seems os.uname()
is returning an invalid value.
When running os.uname()
myself, the result seems legit. Both when the virtualenv is activated or using the os/pyenv python interpeter.
Python 2.7.15 (default, Mar 15 2020, 22:00:51)
[GCC 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.uname()[2].split('.')[0]
'19'
Solutions I've tried:
- Calling
os.uname()
inside and outside of the virtualenv, it works correctly. - Creating a virtualenv using the system Python 2.7.17 installation
- Creating a virtualenvs using pyenv with 3 different python 2 versions (2.7.17, 2.7.16, 2.7.15)
Looking at the full callstack, it seems that ctypes init is being called from the interpeter outside of the virtualenv. This is true both if I'm using the OS's python or the pyenv python when creating the virtualenv. Maybe it's not finding ctypes in the virtualenv?
Traceback (most recent call last):
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/platform/google_appengine/google/appengine/api/lib_config.py", line 358, in __getattr__
self._update_configs()
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/platform/google_appengine/google/appengine/api/lib_config.py", line 294, in _update_configs
self._registry.initialize()
File "/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/platform/google_appengine/google/appengine/api/lib_config.py", line 165, in initialize
import_func(self._modname)
File "/Users/myuser/.pyenv/versions/2.7.15/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/myuser/git/myproject/appengine_config.py", line 38, in <module>
cloud_sql_conn_retry.monkey_patch_mysql_backend()
File "/Users/myuser/git/myproject/server/utils/cloud_sql_conn_retry.py", line 14, in monkey_patch_mysql_backend
from django.db.backends.mysql.base import DatabaseWrapper
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 38, in <module>
from .creation import DatabaseCreation # isort:skip
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/db/backends/mysql/creation.py", line 4, in <module>
from django.db.backends.base.creation import BaseDatabaseCreation
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 5, in <module>
from django.core import serializers
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 23, in <module>
from django.core.serializers.base import SerializerDoesNotExist
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/core/serializers/base.py", line 4, in <module>
from django.db import models
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/db/models/__init__.py", line 3, in <module>
from django.db.models.aggregates import * # NOQA
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/db/models/aggregates.py", line 5, in <module>
from django.db.models.expressions import Func, Star
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/db/models/expressions.py", line 6, in <module>
from django.db.models import fields
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 14, in <module>
from django import forms
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/forms/__init__.py", line 7, in <module>
from django.forms.fields import * # NOQA
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/forms/fields.py", line 17, in <module>
from django.core import validators
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/core/validators.py", line 507, in <module>
allowed_extensions=get_available_image_extensions(),
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/django/core/validators.py", line 498, in get_available_image_extensions
from PIL import Image
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/PIL/Image.py", line 134, in <module>
from pathlib2 import Path
File "/Users/myuser/git/myproject/env/lib/python2.7/site-packages/pathlib2/__init__.py", line 5, in <module>
import ctypes
File "/Users/myuser/.pyenv/versions/2.7.15/lib/python2.7/ctypes/__init__.py", line 29, in <module>
if int(_os.uname()[2].split('.')[0]) < 8:
ValueError: invalid literal for int() with base 10: ''
Also, when importing ctypes in python shell, it's all working. Here is the paths of libraries that are imported while inside the virtual env. ctypes and os are imported from the pyenv interpeter, and django is imported from the virtualenv python. It's not clear to me why, and I suspect this might be related to the issue.
>>> import ctypes
>>> print(ctypes)
<module 'ctypes' from '/Users/myuser/.pyenv/versions/2.7.15/lib/python2.7/ctypes/__init__.pyc'>
>>> import os
>>> print (os)
<module 'os' from '/Users/myuser/.pyenv/versions/2.7.15/lib/python2.7/os.pyc'>
>>> import django
>>> print (django)
<module 'django' from '/Users/myuser/git/myapp/env/lib/python2.7/site-packages/django/__init__.pyc'>
I couldn't find any result on google with the same issue. What could be the issue?
回答1:
The issue was caused by an old version of PIL that configured is configured in app.yaml
. App Engine support out of the box up to version 1.1.17, which doesn't work with the latest python 2 version.
I installed version 5.0.0 of PIL and removed 1.1.17 from the App Engine settings and it solved the problem.
来源:https://stackoverflow.com/questions/60698071/ctypes-init-fails-using-local-app-engine-django-and-virtualenv-with-python-2-7