Search code examples
xonsh

Xonsh error while importing module PyPDF2


I am having trouble importing modules from within xonsh script that are installed in to the virtual environment.

I installed the virtual environment using vox

xontrib load vox
vox activate venv
pip install PyPDF2

Here is my minimal example

#!/usr/bin/env xonsh
import PyPDF2

If I run this from within the Python interpreter it works:

 $ python         
Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyPDF2
>>> 

If I try to run the xonsh script as a script I get the following:

$ ./minimaltest.x
                                                                 sh             
/usr/lib/python3/dist-packages/apport/report.py:13: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import fnmatch, glob, traceback, errno, sys, atexit, locale, imp, stat
Traceback (most recent call last):
  File "/usr/bin/xonsh", line 4, in <module>
    main()
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 24019, in main
    _failback_to_other_shells(args, err)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 23983, in _failback_to_other_shells
    raise err
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 24017, in main
    return main_xonsh(args)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 24060, in main_xonsh
    run_script_with_cache(
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 3043, in run_script_with_cache
    run_compiled_code(ccode, glb, loc, mode)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 2948, in run_compiled_code
    func(code, glb, loc)
  File "./minimaltest.xsh", line 2, in <module>
    import PyPDF2
ModuleNotFoundError: No module named 'PyPDF2'

Any thoughts about what might be wrong?


Solution

  • xonsh can only import modules that exist in the same venv that it is installed in. If you install xonsh into the same venv as PyPDF2 it should work.