Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named '_ctypes' #13

Open
jedie opened this issue Aug 6, 2024 · 3 comments
Open

ModuleNotFoundError: No module named '_ctypes' #13

jedie opened this issue Aug 6, 2024 · 3 comments

Comments

@jedie
Copy link

jedie commented Aug 6, 2024

Try to use psycopg with eGenix PyRun 3.11.9 (release 2.5.0) results in:

Traceback (most recent call last):
  File "<pyrun>/pyrun3.11.py", line 52, in <module>
  File "<pyrun>/pyrun_main.py", line 912, in pyrun_main
  File "<pyrun>/pyrun_main.py", line 813, in pyrun_execute_script
  File "<stdin>", line 1, in <module>
  File "/tmp/pyruntest/lib/python3.11/site-packages/psycopg/__init__.py", line 9, in <module>
    from . import pq  # noqa: F401 import early to stabilize side effects
    ^^^^^^^^^^^^^^^^
  File "/tmp/pyruntest/lib/python3.11/site-packages/psycopg/pq/__init__.py", line 19, in <module>
    from .misc import ConninfoOption, PGnotify, PGresAttDesc
  File "/tmp/pyruntest/lib/python3.11/site-packages/psycopg/pq/misc.py", line 13, in <module>
    import ctypes.util
  File "<pyrun>/ctypes/__init__.py", line 8, in <module>
ModuleNotFoundError: No module named '_ctypes'

script to test this:

#!/bin/bash

set -e

VERSION=3.11

PYRUN=~/.local/bin/pyrun${VERSION}

${PYRUN} -V
${PYRUN} -m venv --without-pip /tmp/pyruntest

ls -la /tmp/pyruntest/bin
/tmp/pyruntest/bin/pyrun${VERSION} -V

# pyrun error: Could not run 'ensurepip': No module named ensurepip
#/tmp/pyruntest/bin/pyrun${VERSION} -m ensurepip

# pyrun error: Could not run 'pip': No module named pip
# /tmp/pyruntest/bin/pyrun${VERSION} -m pip install --upgrade pip

cd /tmp
wget --timestamp https://bootstrap.pypa.io/get-pip.py

/tmp/pyruntest/bin/pyrun${VERSION} /tmp/get-pip.py

/tmp/pyruntest/bin/pyrun${VERSION} -m pip -V
/tmp/pyruntest/bin/pyrun${VERSION} -m pip install --upgrade pip

/tmp/pyruntest/bin/pyrun${VERSION} -m pip install -U psycopg[binary]

/tmp/pyruntest/bin/pyrun${VERSION} -c "import psycopg; print(psycopg.__version__)"
@malemburg
Copy link
Contributor

The ctypes module is currently not built directly into the pryrun executable, but only as a shared lib. You can find the needed shared lib in the distribution archives under lib/python3.11/lib-dynlib/,

Copying the lib-dynlib/ dir to /tmp/pyruntest/ should get things working. If you only need ctypes, you can also copy the _ctypes*.so file into the same dir where pyrun3.11 lives. This should normally also work.

I'm a bit hesitant to include ctypes into the pyrun binary, since this introduces external dependencies on libffi, which may not be available everywhere (but I'm happy to be proven wrong on that one).

@jedie
Copy link
Author

jedie commented Oct 9, 2024

What does https://github.com/indygreg/python-build-standalone/ do in this direction?

@malemburg
Copy link
Contributor

malemburg commented Oct 9, 2024

What does https://github.com/indygreg/python-build-standalone/ do in this direction?

It seems to include a statically linked version into the binary.

I checked on various systems I have access to:

  • I found three different versions of libffi (6, 7, and 8)
  • some systems did not have the lib at all

This makes it a rather poor candidate for inclusion as a shared dependency.

I could investigate statically linking against libffi. It's a rather small lib, so this may be an option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants