"No module named pybind11" after installing pybind11
Asked Answered
D

2

8

I have installed pybind11 (several different ways, including pip and the package manager) on my Ubuntu 18.04.3 machine, but when I try to specify include files the recommended way:

python3 -m pybind11 --includes

I get this error:

/usr/bin/python3: No module named pybind11.__main__; 'pybind11' is a package and cannot be directly executed

The only place I've found this error mentioned is a Chinese web page which wasn't helpful in resolving the problem. How do I get pybind11 to work?

To answer some of the questions below:

>>> import pybind11; print(pybind11.__file__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pybind11' has no attribute '__file__'
> python3 -m pip install pybind11
WARNING: The directory '/home/<username>/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: pybind11 in /usr/local/lib/python3.6/dist-packages (2.5.0)
> sudo pip3 -V
pip 20.0.2 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)
> python3 -V
Python 3.6.8
Dim answered 15/4, 2020 at 18:13 Comment(9)
Can you do import pybind11; print(pybind11.__file__) in a python interpreter? Since you've installed it "several different ways", I wonder which one Python is using.Millisecond
Does this answer your question? Execution of Python code with -m option or notAnthem
Ubuntu may have many Pythons installed - so first check if python3 -V and pip3 -V gives the same number. And then install it with pip3. Or use python3.7 a pip3.7 to work with Python 3.7 (similar for other versions: python3.6 and pip3.6, etc). OR you can install using directly python which you use to run code : python3 -m pip install pybind11Salvidor
I tested it on Linux Mint 19.3 (based on Ubuntu 18.04) sudo pip3.7 install -U pybind11 and python3.7 -m pybind11 --includes and it runs without error.Salvidor
I have edited the question to answer the questions asked above as best I can, thanks!Dim
Hello, sometimes at windows there seems some libs have this error. I've fixed it by adding python scripts at user environment variables. Does linux have that? (for windows from miniconda: c:\Users\username\AppData\Roaming\Python\Python37\Scripts)Qadi
Are you using virtual environments or something similar?Jovitta
No, I'm not using an environment.Dim
It is a common problem in clever environments like Linux distribs. For any reason, you end with more than one single version of Python installed in your system. And you installed pybind11 under one version and try to use it from a different one. Search all python* files under /bin, /usr/bin/, /usr/local/bin. If you have more than one, it will be a hint that you fell in this problem. The key when it happens is to use the full name of the python version that you want to use, and use pythonxx -m pip instead of just pip to make sure to install the packages into the expected version.Supernational
K
21

pybind11 was already installed. Fasttext installation was not able to find pybind11 until I ran this:

pip install wheel setuptools pip --upgrade

Kirima answered 30/11, 2023 at 1:40 Comment(1)
This was useful insightStlouis
J
1

try this command.

export PYTHONPATH=$PYTHONPATH:/home/xxx/pybind11
Janinejanis answered 21/12, 2020 at 8:26 Comment(1)
This did not help for me.Harlow

© 2022 - 2025 — McMap. All rights reserved.