import pyodbc failure mac os with unixodbc already installed [duplicate] - python

I have followed official tutorial for installing ODBC driver and installed pydobc with exporting the flags and updating odbc files with drivers location. I am able to print pyodbc.drivers() and find them
I have a python venv where I pip installed pyodbc and with success I can
python3.9 -m venv .venv
source .venv/bin/activate
pip install pyodbc
> installed 4.0.32
python
import pyodbc
pyodbc.drivers() # locates and prints with a success
When I try to host a default code azure function it's a success. But when I add import pyodbc at the top of the file, it fails.
Expected behaviour:
Function should run and import pyodbc
Actual behaviour:
It fails, stack trace below.
Specs:
Python: 3.9.13
Core Tools Version 3.0.4585 Commit hash: N/A (64-bit)
Function Runtime Version: 3.7.1.0
Mac os M1 12.4
For detailed output, run func with --verbose flag.
[2022-07-09T15:45:22.308Z] Traceback (most recent call last):
[2022-07-09T15:45:22.314Z] File "/opt/homebrew/Cellar/azure-functions-core-tools#3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/bindings/shared_memory_data_transfer/file_accessor_unix.py", line 127, in _get_valid_mem_map_dirs
[2022-07-09T15:45:22.315Z] os.makedirs(dir_path)
[2022-07-09T15:45:22.315Z] File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 215, in makedirs
[2022-07-09T15:45:22.315Z] makedirs(head, exist_ok=exist_ok)
[2022-07-09T15:45:22.315Z] File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py", line 225, in makedirs
[2022-07-09T15:45:22.315Z] mkdir(name, mode)
[2022-07-09T15:45:22.315Z] PermissionError: [Errno 1] Operation not permitted: '/dev/shm'
[2022-07-09T15:45:22.629Z] Worker process started and initialized.
[2022-07-09T15:45:23.001Z] Worker failed to function id 716b2b93-8695-4822-ad71-cd76c3035b3c.
[2022-07-09T15:45:23.006Z] Result: Failure
[2022-07-09T15:45:23.006Z] Exception: ImportError: dlopen(/Users/bartoszlachowicz/Desktop/CodeProjects/customers-slack-app/.venv/lib/python3.9/site-packages/pyodbc.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '_SQLAllocHandle'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
[2022-07-09T15:45:23.006Z] Stack: File "/opt/homebrew/Cellar/azure-functions-core-tools#3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/dispatcher.py", line 305, in _handle__function_load_request
[2022-07-09T15:45:23.006Z] func = loader.load_function(
[2022-07-09T15:45:23.006Z] File "/opt/homebrew/Cellar/azure-functions-core-tools#3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call
[2022-07-09T15:45:23.006Z] raise extend_exception_message(e, message)
[2022-07-09T15:45:23.006Z] File "/opt/homebrew/Cellar/azure-functions-core-tools#3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
[2022-07-09T15:45:23.006Z] return func(*args, **kwargs)
[2022-07-09T15:45:23.006Z] File "/opt/homebrew/Cellar/azure-functions-core-tools#3/3.0.4585/workers/python/3.9/OSX/X64/azure_functions_worker/loader.py", line 85, in load_function
[2022-07-09T15:45:23.006Z] mod = importlib.import_module(fullmodname)
[2022-07-09T15:45:23.006Z] File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
[2022-07-09T15:45:23.006Z] return _bootstrap._gcd_import(name[level:], package, level)
[2022-07-09T15:45:23.006Z] File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
[2022-07-09T15:45:23.006Z] File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
[2022-07-09T15:45:23.006Z] File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
[2022-07-09T15:45:23.006Z] File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
[2022-07-09T15:45:23.006Z] File "<frozen importlib._bootstrap_external>", line 850, in exec_module
[2022-07-09T15:45:23.006Z] File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
[2022-07-09T15:45:23.006Z] File "/Users/bartoszlachowicz/Desktop/CodeProjects/customers-slack-app/__app__/overview/__init__.py", line 9, in <module>
[2022-07-09T15:45:23.006Z] import pyodbc
[2022-07-09T15:45:23.007Z] .
Update
Importing pyodbc fails when using python3.7.4 and running python file
import pyodbc
<ImportError: dlopen(/Users/bartoszlachowicz/Desktop/CodeProjects/new/.venv/lib/python3.7/site-packages/pyodbc.cpython-37m-darwin.so, 0x0002): symbol not found in flat namespace '_SQLAllocHandle'

After reproducing from our end, We also faced the same issue and noticed that the ODBC drivers were missing. Installing the ODBC Drivers made this work.
REFERENCES:
Install the Microsoft ODBC driver for SQL Server (macOS)

Related

Error "ModuleNotFoundError: No module named 'psycopg2'" when deploying Django app to Google App Engine

I'm working on a Django application and using Fabric for deployment.
When I deployed the apps to staging in Google App Engine using fab testing, I got this error:
Updating service [staging] (this may take several minutes)...failed.
ERROR: (gcloud.app.deploy) Error Response: [9] An internal error occurred while processing task /app-engine-flex/flex_await_healthy/flex_await_healthy>2022-01-01T09:48:30.226Z15496.fj.0: Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute
django.setup()
File "/env/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/env/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/env/lib/python3.6/site-packages/django/apps/config.py", line 116, in create
mod = import_module(mod_path)
File "/opt/python3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/env/lib/python3.6/site-packages/django/contrib/postgres/apps.py", line 8, in <module>
from .signals import register_type_handlers
File "/env/lib/python3.6/site-packages/django/contrib/postgres/signals.py", line 3, in <module>
import psycopg2
ModuleNotFoundError: No module named 'psycopg2'
I'm sure the psycopg2 has been installed successfully earlier. Here's the list installed dependencies after checking by pip list:
...
platformdirs 2.4.0
prompt-toolkit 3.0.24
protobuf 3.19.1
psutil 5.5.1
psycopg2 2.8.6
pyasn1 0.4.8
pyasn1-modules 0.2.8
pycairo 1.16.2
...
Anyone can help? Thanks!
Thank you guys for your prompt support. I've managed to resolve it.
Basically during coding I've accidentally commented the psycopg2 line in the requirements.txt. Because earlier all dependencies were installed successfully and everything is running on dev mode properly so I just kept that line excluded out.
But when deploying with Fabric, it will execute the gcloud command (gcloud app deploy) and here's the thing: GCloud will re-run everything (including installing all dependencies all over again). And the psycopg2 wasn't installed (because it's commented out) during that GCloud runtime. That's why GCloud couldn't find it for further executions.
So I've put it back and everything works like a charm. What a stupid move of mine, but it's fantastic to understand how GCloud works on its way
Just add in your requirement.txt to psycopg2=2.9.3
or run the below command manually to your virtual environment
pip install psycopg2

OSError: libgomp.so.1 not found when importing gluoncv through azure app services deployment

I have a web app that I am trying to deploy to azure. After fixing various issues I am stuck with as pasted below.
I am using python 3.7 and I have windows 10. Trying to deploy a flask app with multiple computer vision models. I created a virtual env via the azure cli, installed my requiremnts.txt and then deployed it. It does deploy successfully but when I checkout the website it says "Application Error. The below is what I see when I check the logs. So far I have tried somewhat what I found over here but a solution is still unreachable:
https://github.com/business-science/modeltime.gluonts/issues/32
Could torch fix this issue? Just wondering.
2021-12-01T10:47:07.975121283Z Traceback (most recent call last):
2021-12-01T10:47:07.975390586Z File "/opt/python/3.7.9/lib/python3.7/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2021-12-01T10:47:07.975445787Z worker.init_process()
2021-12-01T10:47:07.975466487Z File "/opt/python/3.7.9/lib/python3.7/site-packages/gunicorn/workers/base.py", line 134, in init_process
2021-12-01T10:47:07.975473287Z self.load_wsgi()
2021-12-01T10:47:07.975478588Z File "/opt/python/3.7.9/lib/python3.7/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2021-12-01T10:47:07.975484588Z self.wsgi = self.app.wsgi()
2021-12-01T10:47:07.975489588Z File "/opt/python/3.7.9/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-12-01T10:47:07.975495488Z self.callable = self.load()
2021-12-01T10:47:07.975665490Z File "/opt/python/3.7.9/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2021-12-01T10:47:07.975673090Z return self.load_wsgiapp()
2021-12-01T10:47:07.975678990Z File "/opt/python/3.7.9/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2021-12-01T10:47:07.975686090Z return util.import_app(self.app_uri)
2021-12-01T10:47:07.975691690Z File "/opt/python/3.7.9/lib/python3.7/site-packages/gunicorn/util.py", line 359, in import_app
2021-12-01T10:47:07.975698190Z mod = importlib.import_module(module)
2021-12-01T10:47:07.975704290Z File "/opt/python/3.7.9/lib/python3.7/importlib/__init__.py", line 127, in import_module
2021-12-01T10:47:07.975778791Z return _bootstrap._gcd_import(name[level:], package, level)
2021-12-01T10:47:07.975785391Z File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
2021-12-01T10:47:07.975792491Z File "<frozen importlib._bootstrap>", line 983, in _find_and_load
2021-12-01T10:47:07.975798791Z File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
2021-12-01T10:47:07.975804591Z File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
2021-12-01T10:47:07.975810291Z File "<frozen importlib._bootstrap_external>", line 728, in exec_module
2021-12-01T10:47:07.975816592Z File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-12-01T10:47:07.975822592Z File "/tmp/8d9b4b3d8465f24/app.py", line 6, in <module>
2021-12-01T10:47:07.975828692Z import cv_ins
2021-12-01T10:47:07.975834792Z File "/tmp/8d9b4b3d8465f24/cv_ins.py", line 9, in <module>
2021-12-01T10:47:07.975840792Z import gluoncv
2021-12-01T10:47:07.975846292Z File "/tmp/8d9b4b3d8465f24/antenv/lib/python3.7/site-packages/gluoncv/__init__.py", line 16, in <module>
2021-12-01T10:47:07.975852292Z _require_mxnet_version('1.4.0', '2.0.0')
2021-12-01T10:47:07.975858292Z File "/tmp/8d9b4b3d8465f24/antenv/lib/python3.7/site-packages/gluoncv/check.py", line 6, in _require_mxnet_version
2021-12-01T10:47:07.975867092Z import mxnet as mx
2021-12-01T10:47:07.975882692Z File "/tmp/8d9b4b3d8465f24/antenv/lib/python3.7/site-packages/mxnet/__init__.py", line 23, in <module>
2021-12-01T10:47:07.975888892Z from .context import Context, current_context, cpu, gpu, cpu_pinned
2021-12-01T10:47:07.975894292Z File "/tmp/8d9b4b3d8465f24/antenv/lib/python3.7/site-packages/mxnet/context.py", line 23, in <module>
2021-12-01T10:47:07.975901293Z from .base import classproperty, with_metaclass, _MXClassPropertyMetaClass
2021-12-01T10:47:07.975907193Z File "/tmp/8d9b4b3d8465f24/antenv/lib/python3.7/site-packages/mxnet/base.py", line 351, in <module>
2021-12-01T10:47:07.975912693Z _LIB = _load_lib()
2021-12-01T10:47:07.975917793Z File "/tmp/8d9b4b3d8465f24/antenv/lib/python3.7/site-packages/mxnet/base.py", line 342, in _load_lib
2021-12-01T10:47:07.975923593Z lib = ctypes.CDLL(lib_path[0], ctypes.RTLD_LOCAL)
2021-12-01T10:47:07.975929293Z File "/opt/python/3.7.9/lib/python3.7/ctypes/__init__.py", line 364, in __init__
2021-12-01T10:47:07.975935793Z self._handle = _dlopen(self._name, mode)
2021-12-01T10:47:07.975941293Z OSError: libgomp.so.1: cannot open shared object file: No such file or directory
My requirements.txt:
Flask==2.0.1
gluoncv==0.10.4.post1
imutils==0.5.4
ipywidgets==7.6.3
matplotlib==3.4.2
mxnet==1.7.0.post2
numpy
numpydoc
opencv-python==4.5.3.56
pandas==1.3.1
py==1.11.0
schedule==1.1.0
Werkzeug==2.0.1
zipp
You can fix this OSError: libgomp.so.1: cannot open shared object file: No such file or directory OSError by installing libgomp1
For Windows:
This error could be due to lack of not finding dependencies of OpenCV/gluoncv
You can refer to similar issue on Microsoft Q&A: OSError: libgomp.so.1: cannot open shared object file: No such file or directory
For Linux:
sudo apt-get install libgomp1
You can refer to libgomp.so.1: cannot open shared object file: No such file or directory

Tensorflow on Raspberry Pi Error

I have a python script that works without any errors on my desktop. When I try to run it on my raspberry pi, I encounter an error.
To install tensorflow on the raspberry pi for python 3.5, I followed this tutorial: https://petewarden.com/2017/08/20/cross-compiling-tensorflow-for-the-raspberry-pi/ There is no offical binary for tensorflow and python 3.5 and I had errors when I tried to compile it myself. The tutorial suggest to just install tensorflow from the Python 3.4 binary.
If you’re running Python 3.5, you can use the Python 3.4 wheel but with a
slight change to the file name, since that encodes the version. You
will see a couple of warnings every time you import tensorflow, but it
should work correctly.
Here is the error I am encountering:
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.4 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.5
return f(*args, **kwds)
/usr/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: builtins.type size changed, may indicate binary incompatibility. Expected 432, got 412
return f(*args, **kwds)
Traceback (most recent call last):
File "run.py", line 93, in <module>
model = FastPredict(tf.contrib.learn.Estimator(model_fn=convolutional_neural_network, model_dir="tmp"))
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/python/util/lazy_loader.py", line 53, in __getattr__
module = self._load()
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/python/util/lazy_loader.py", line 42, in _load
module = importlib.import_module(self.__name__)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 673, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/contrib/__init__.py", line 82, in <module>
from tensorflow.contrib.eager.python import tfe as eager
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/contrib/eager/python/tfe.py", line 76, in <module>
from tensorflow.contrib.eager.python.datasets import Iterator
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/contrib/eager/python/datasets.py", line 23, in <module>
from tensorflow.contrib.data.python.ops import prefetching_ops
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/contrib/data/python/ops/prefetching_ops.py", line 25, in <module>
resource_loader.get_path_to_datafile("../../_prefetching_ops.so"))
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/contrib/util/loader.py", line 55, in load_op_library
ret = load_library.load_op_library(path)
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/python/framework/load_library.py", line 56, in load_op_library
lib_handle = py_tf.TF_LoadLibrary(library_filename, status)
File "/home/pi/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.NotFoundError: /home/pi/.local/lib/python3.5/site-packages/tensorflow/contrib/data/python/ops/../../_prefetching_ops.so: undefined symbol: _ZN6google8protobuf8internal26fixed_address_empty_stringE
Line 93 attempts to create an Estimator which causes the error.
tf.contrib.learn.Estimator(model_fn=convolutional_neural_network, model_dir="tmp")
I found a similar error on github but it was no help: https://github.com/tensorflow/serving/issues/684
I installed an older build. It doesn't cause this error. http://ci.tensorflow.org/view/Nightly/job/nightly-pi/78/artifact/output-artifacts/tensorflow-1.3.0-cp27-none-any.whl

Trouble with psycopg2 setting up django

I'm trying to set up a Django app using a postgresql server (to eventually use with GeoDjango for GIS). I'm on a mac running OSX 10.11 and am running python 3.5 in a virtualenv. I've gone through all the installtion of postgresql, pyscopg2 etc and am trying to run
python manage.py migrate
But when I do I get the following error.
Traceback (most recent call last):
File "/Users/me/.python3env/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 20, in <module>
import psycopg2 as Database
File "/Users/me/.python3env/lib/python3.5/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Users/me/.python3env/lib/python3.5/site-packages/psycopg2/_psycopg.cpython-35m-darwin.so, 2): Library not loaded: libssl.1.0.0.dylib
Referenced from: /Users/me/.python3env/lib/python3.5/site-packages/psycopg2/_psycopg.cpython-35m-darwin.so
Reason: image not found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/me/.python3env/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/me/.python3env/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/Users/me/.python3env/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/me/.python3env/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/me/.python3env/lib/python3.5/site-packages/django/apps/config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "/Users/me/.python3env/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/me/.python3env/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/me/.python3env/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Users/me/.python3env/lib/python3.5/site-packages/django/db/models/base.py", line 119, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/Users/me/.python3env/lib/python3.5/site-packages/django/db/models/base.py", line 316, in add_to_class
value.contribute_to_class(cls, name)
File "/Users/me/.python3env/lib/python3.5/site-packages/django/db/models/options.py", line 214, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Users/me/.python3env/lib/python3.5/site-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Users/me/.python3env/lib/python3.5/site-packages/django/db/utils.py", line 211, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Users/me/.python3env/lib/python3.5/site-packages/django/db/utils.py", line 115, in load_backend
return import_module('%s.base' % backend_name)
File "/Users/me/.python3env/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/Users/me/.python3env/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 24, in <module>
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: dlopen(/Users/me/.python3env/lib/python3.5/site-packages/psycopg2/_psycopg.cpython-35m-darwin.so, 2): Library not loaded: libssl.1.0.0.dylib
Referenced from: /Users/me/.python3env/lib/python3.5/site-packages/psycopg2/_psycopg.cpython-35m-darwin.so
Reason: image not found
I've found a couple of stackoverflow threads on this error but none of the solutions seem to help at all.
Any advice would be much appreciated!
Edit: brew doctor output
Warning: python is symlinked to python3
This will confuse build scripts and in general lead to subtle breakage.
Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and what additional flags to use when
compiling and linking.
Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew provided
script of the same name. We found the following "config" scripts:
/Users/me/.python3env/bin/python-config
Warning: Python is installed at /Library/Frameworks/Python.framework
Homebrew only supports building against the System-provided Python or a
brewed Python. In particular, Pythons installed to /Library can interfere
with other software installs.

Error running Python 3.4 with PostgreSQL (psycopg2) and Django

So if I start Python 2.7's shell with python, and run import psycopg2, I don't receive any errors. However, if I try to run import psycopg2 in Python 3.4's shell, I get: ImportError: No module named 'psycopg2'. On the other hand, importing django works fine on both shells (after using pip3 to install it).
Secondly, if I run python3 manage.py runserver, I get an error (see below), whereas I don't if I use just python. I think this is because of the first problem I described but I'm not sure (sorry if this is a stupid question, I'm really new to Django - trying to get through the tutorial).
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 23, in < module >
import psycopg2 as Database
ImportError: No module named 'psycopg2'
During handling of the above exception, another exception occurred:
Traceback(most recent call last):
File "manage.py", line 10, in < module >
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level: ], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/contrib/auth/models.py", line 40, in < module >
class Permission(models.Model):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, * * kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/base.py", line 297, in add_to_class
value.contribute_to_class(cls, name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/models/options.py", line 166, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/__init__.py", line 40, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 242, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/utils.py", line 108, in load_backend
return import_module('%s.base' % backend_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level: ], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 27, in < module >
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading psycopg2 module: No module named 'psycopg2'
What can I do to fix it?
Additionally, I'm super confused on this whole Python 2 vs 3 business with Django (sorry again if this is a really basic question). When you run django-admin.py startproject theproject, how do you specify which version of Python you want to use with Django? Is it as simple as just writing your code in Python 3 and running everything with python3 as opposed to python?
Make sure you have run pip3 install psycopg2.
By default, django-admin.py will run using Python 2. That doesn't matter, since all it's doing is creating the folder layout the project.
Once the project has been created, you can write Python 3 code, and run your site using python3 manage.py runserver to use Python 3.
Later on, you might want to investigate virtual environments. If you create your virtual environment to use Python 3
virtualenv v -p python3
Then you can run
./manage.py runserver
in your virtual environment, and it will use Python 3.
I ran into the same problem as I switched from using Python 3 from Python 2.7 using virtualenv on a Mac. Apparently, while trying to pip install psycopg2 from Python 3 it was still using the wheel created during the install made using Python 2.7. So I had to rebuild the wheel using
sudo -H pip3 install --upgrade --force-reinstall psycopg2
If you have multiple python versions installed then the best solution to install the psycopg is to download the archive from
http://initd.org/psycopg/download/
and then install in by running (in my case, I wanted to install it for python 3.4)
python3.4 setup.py build
sudo python3.4 setup.py install
Now, check if it is installed by typing:
python3.4
help()
modules
It should display psycopg2 in the list of modules.

Categories

Resources