couchbase is installed but still getting import error (Pyinstaller)

1.9k views Asked by At

I am trying to build python source code using pyinstaller. Once Building is successful I am running the binary file. Once I run the binary file I am getting the following ImportError:

PyUtils.CouchbaseClient", line 13, in <module>
ImportError: No module named couchbase

But couchbase is already installed and I am able to run the original Python source code without any import errors. After converting into the binary I am getting ImportError.

Any help would be really appreciated.

1

There are 1 answers

6
Yoel On

Since your program dynamically imports couchbase, pyinstaller can't detect that it is required, as specified in the documentation:

Some Python scripts import modules in ways that PyInstaller cannot detect: for example, by using the __import__() function with variable data, or manipulating the sys.path value at run time.

Another section of the documentation extends on this issue and suggests some workarounds for dealing with this scenario:

If Analysis thinks it has found all the imports, but the app fails with an import error, the problem is a hidden import; that is, an import that is not visible to the analysis phase.

Hidden imports can occur when the code is using __import__ or perhaps exec or eval. You get warnings of these (see Build-time Messages).

Hidden imports can also occur when an extension module uses the Python/C API to do an import. When this occurs, Analysis can detect nothing. There will be no warnings, only a crash at run-time.

To find these hidden imports, set the -v flag (Getting Python's Verbose Imports above).

Once you know what they are, you add the needed modules to the bundle using the --hidden-import= command option, by editing the spec file, or with a hook file (see Using Hook Files below).

Therefore, you need to explicitly ask pyinstaller to include the required module during the build process. It seems that the easiest way to do so is with the --hidden-import argument:

--hidden-import=modulename
Name an imported Python module that is not visible in your code. The module will be included as if it was named in an import statement. This option can be given more than once.

Thus, please add the following argument when building the application:

--hidden-import=couchbase


However, once PyInstaller detects that couchbase is required, running the resulting executable fails with an ImportError:

ImportError: No module named _libcouchbase

This answer describes what should be done in order to resolve it.