简体   繁体   中英

ModuleNotFoundError: No module named 'dlib' in pipenv

Sorry I am new to all this. I can't install dlib inside pipenv virtual environment, however, I able to install and import it outside pipenv virtual env.

When I navigate to the folder containing my code and type pipenv shell and then python3 example.py which is the code I want to run, I get this error:

import dlib
ModuleNotFoundError: No module named 'dlib'

Then I tried to install dlib using these commands:

$ sudo apt-get update
$ sudo apt-get install build-essential cmake
$ sudo apt-get install libopenblas-dev liblapack-dev 
$ sudo apt-get install libx11-dev libgtk-3-dev
$ sudo apt-get install python python-dev python-pip
$ sudo apt-get install python3 python3-dev python3-pip
$ pip3 install dlib

everything worked fine except the last command, here is the output I am getting, in red:

$ pip3 install dlib
Collecting dlib
  Using cached dlib-19.19.0.tar.gz (3.2 MB)
Building wheels for collected packages: dlib
  Building wheel for dlib (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/hany/.local/share/virtualenvs/vesselware-rtITNVcC/bin/python3.5m -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-24bjeekz/dlib/setup.py'"'"'; __file__='"'"'/tmp/pip-install-24bjeekz/dlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-3n4hx6v_
       cwd: /tmp/pip-install-24bjeekz/dlib/
  Complete output (292 lines):
  running bdist_wheel
  running build
  running build_py
  package init file 'dlib/__init__.py' not found (or not a regular file)
  warning: build_py: byte-compiling is disabled, skipping.

  running build_ext
  Building extension for Python 3.5.9 (default, Nov 24 2019, 01:35:13)
  Invoking CMake setup: 'cmake /tmp/pip-install-24bjeekz/dlib/tools/python -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/tmp/pip-install-24bjeekz/dlib/build/lib.linux-x86_64-3.5 -DPYTHON_EXECUTABLE=/home/hany/.local/share/virtualenvs/vesselware-rtITNVcC/bin/python3.5m -DCMAKE_BUILD_TYPE=Release'

/tmp/pip-install-24bjeekz/dlib/dlib/external/pybind11/include/pybind11/detail/common.h:111:10: fatal error: Python.h: No such file or directory
     #include <Python.h>
              ^~~~~~~~~~
    compilation terminated.
    CMakeFiles/dlib_python.dir/build.make:134: recipe for target 'CMakeFiles/dlib_python.dir/src/svm_c_trainer.cpp.o' failed
    make[2]: *** [CMakeFiles/dlib_python.dir/src/svm_c_trainer.cpp.o] Error 1
    CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/dlib_python.dir/all' failed
    make[1]: *** [CMakeFiles/dlib_python.dir/all] Error 2
    Makefile:83: recipe for target 'all' failed
    make: *** [all] Error 2
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-24bjeekz/dlib/setup.py", line 261, in <module>
        'Topic :: Software Development',
      File "/home/hany/.local/share/virtualenvs/vesselware-rtITNVcC/lib/python3.5/site-packages/setuptools/__init__.py", line 145, in setup
        return distutils.core.setup(**attrs)
      File "/usr/lib/python3.5/distutils/core.py", line 148, in setup
        dist.run_commands()
      File "/usr/lib/python3.5/distutils/dist.py", line 955, in run_commands
        self.run_command(cmd)
      File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
        cmd_obj.run()
      File "/home/hany/.local/share/virtualenvs/vesselware-rtITNVcC/lib/python3.5/site-packages/setuptools/command/install.py", line 61, in run
        return orig.install.run(self)
      File "/usr/lib/python3.5/distutils/command/install.py", line 583, in run
        self.run_command('build')
      File "/usr/lib/python3.5/distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
        cmd_obj.run()
      File "/usr/lib/python3.5/distutils/command/build.py", line 135, in run
        self.run_command(cmd_name)
      File "/usr/lib/python3.5/distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/usr/lib/python3.5/distutils/dist.py", line 974, in run_command
        cmd_obj.run()
      File "/tmp/pip-install-24bjeekz/dlib/setup.py", line 135, in run
        self.build_extension(ext)
      File "/tmp/pip-install-24bjeekz/dlib/setup.py", line 175, in build_extension
        subprocess.check_call(cmake_build, cwd=build_folder)
      File "/usr/lib/python3.5/subprocess.py", line 271, in check_call
        raise CalledProcessError(retcode, cmd)
    subprocess.CalledProcessError: Command '['cmake', '--build', '.', '--config', 'Release', '--', '-j4']' returned non-zero exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/hany/.local/share/virtualenvs/vesselware-rtITNVcC/bin/python3.5m -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-24bjeekz/dlib/setup.py'"'"'; __file__='"'"'/tmp/pip-install-24bjeekz/dlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-ifngrgm8/install-record.txt --single-version-externally-managed --compile --install-headers /home/hany/.local/share/virtualenvs/vesselware-rtITNVcC/include/site/python3.5/dlib Check the logs for full command output.

When I tried doing the same thing outside the virtual env, everything worked fine.

I am out of ideas, I very much appreciate your advice.

Edit 1

I tried installing dlib within the pipenv virtual environment using conda, here is the result:

Launching subshell in virtual environment…
 . /home/hano/.local/share/virtualenvs/vesselware-rtITNVcC/bin/activate
hano@hano-asus:~/scripts/vesselware$  . /home/hano/.local/share/virtualenvs/vesselware-rtITNVcC/bin/activate
(vesselware) hano@hano-asus:~/scripts/vesselware$ conda install -c menpo dlib
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Solving environment: | 
Found conflicts! Looking for incompatible packages.
This can take several minutes.  Press CTRL-C to abort.
failed                                                                                                                                                                  

UnsatisfiableError: The following specifications were found                                                                                                             
to be incompatible with the existing python installation in your environment:

Specifications:

  - dlib -> python[version='2.7.*|3.5.*|>=3.5,<3.6.0a0']
  - dlib -> python[version='>=2.7,<2.8.0a0']

Your python: python=3.7

If python is on the left-most side of the chain, that's the version you've asked for.
When python appears to the right, that indicates that the thing on the left is somehow
not available for the python version you are constrained to. Note that conda will not
change your python version to a different minor version unless you explicitly specify
that.

The following specifications were found to be incompatible with each other:



Package certifi conflicts for:
python=3.7 -> pip -> setuptools -> certifi[version='>=2016.09|>=2016.9.26']
dlib -> python=3.5 -> pip -> setuptools -> certifi[version='>=2016.09|>=2016.9.26']
Package wheel conflicts for:
dlib -> python=3.5 -> pip -> wheel
python=3.7 -> pip -> wheel
Package pip conflicts for:
dlib -> python=3.5 -> pip
python=3.7 -> pip
Package ca-certificates conflicts for:
python=3.7 -> openssl[version='>=1.1.1a,<1.1.2a'] -> ca-certificates
dlib -> python=3.5 -> ca-certificates
Package setuptools conflicts for:
python=3.7 -> pip -> setuptools
dlib -> python=3.5 -> pip -> setuptools

Edit 2

I made sure that pipenv uses python 3.5 (as you can see in the pip3 error above), but I still can't install dlib neither by pip3 nor by conda. And the strange thing is that conda error tells me Your python: python=3.7 while pip3 error states clearly that I am using python 3.5: Building extension for Python 3.5.9

Please advice.

Try the following:

conda create --name python3_5 python=3.5
conda activate python3_5
conda install -c menpo dlib

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM