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

[BUG] ImportError: ImportError: __import__ not found Python 3.7 #2557

Closed
ObjatieGroba opened this issue Oct 7, 2020 · 5 comments · Fixed by #2616
Closed

[BUG] ImportError: ImportError: __import__ not found Python 3.7 #2557

ObjatieGroba opened this issue Oct 7, 2020 · 5 comments · Fixed by #2616

Comments

@ObjatieGroba
Copy link

ObjatieGroba commented Oct 7, 2020

I used python_example (https://github.com/pybind/python_example) to create new package.
After installation I have

>>> import mymodule
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: ImportError: __import__ not found

At:
  <string>(3): <module>
  <frozen importlib._bootstrap>(222): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(914): create_module
  <frozen importlib._bootstrap>(577): module_from_spec
  <frozen importlib._bootstrap>(666): _load_unlocked
  <frozen importlib._bootstrap>(958): _find_and_load_unlocked
  <frozen importlib._bootstrap>(969): _find_and_load
  <stdin>(1): <module>

On python3.8 works perfectly

Reproducible example code

Debian10. Python 3.7

Clone and install python_example

Add this code to main.cpp

auto global = m.attr("__dict__");
py::exec(R"(
    import contextlib
)", global);

EDIT (@YannickJadoul): "After reading, remove this checklist and the template text in parentheses below."

@henryiii
Copy link
Collaborator

henryiii commented Oct 8, 2020

Why is this installing an egg? You should be using wheels. pip install ., never python setup.py install or family - installing with python setup.py install is deprecated and discouraged heavily by the Python Packaging Authority. If you run setup.py and never import setuptools, you get a pure distutils build, which is likely broken. And if you don't run it with pip, pip can't insert shims needed for building.

Python devs pretty much will not touch distutils, even to fix it, and are considering getting rid of it entirely. Setuptools now vendors distutils.

@ObjatieGroba
Copy link
Author

Why is this installing an egg? You should be using wheels. pip install ., never python setup.py install or family - installing with python setup.py install is deprecated and discouraged heavily by the Python Packaging Authority. If you run setup.py and never import setuptools, you get a pure distutils build, which is likely broken. And if you don't run it with pip, pip can't insert shims needed for building.

Python devs pretty much will not touch distutils, even to fix it, and are considering getting rid of it entirely. Setuptools now vendors distutils.

Hi, thank you.

pip install . leads to the same error.

cmake_example uses setuptoools

What should I do to install it?

@ObjatieGroba
Copy link
Author

I found it.

auto global = m.attr("__dict__");
py::exec(R"(
    import contextlib
)", global);

This code does not working on Python 3.7 and earlier

@henryiii
Copy link
Collaborator

henryiii commented Oct 8, 2020

This code is not part of pybind11 or the examples, as far as I can tell?

Ah, you edited the description, sorry.

@henryiii
Copy link
Collaborator

henryiii commented Oct 8, 2020

I don't think that's the right way to import something. You should do py::module::import("contextlib"). Certainly, it's not valid to put a module's __dict__ in as a replacement for the global context.

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

Successfully merging a pull request may close this issue.

2 participants