I'm trying to figure out why a 3rd party module import no longer works after chmod +x

The python script works when it’s not an executable. After chmod +x, I get this error. The traceback is just the import statement.

import drawsvg
  ModuleNotFoundError: No module named 'drawsvg'

I tried both of these shebang lines.

#!/usr/bin/python
#!/usr/bin/python3

The python path is set to the conda environment’s. However, I also installed drawsvg for the entire system.

$ which python
/home/sl/anaconda3/envs/project/bin/python
$ which python3
/home/sl/anaconda3/envs/project/bin/python3

Whether I’m in the regular system, conda base, or conda project, the python version is the same.

$ python --version
Python 3.12.2
$ python3 --version
Python 3.12.2

Whether I’m in the regular system, conda base, or conda project, drawsvg always shows the same way.

$ pip show drawsvg
Name: drawsvg
Version: 2.4.0
Summary: A Python 3 library for programmatically generating SVG (vector) images and animations.  Drawsvg can also render to PNG, MP4, and display your drawings in Jupyter notebook and Jupyter lab.
Home-page: <https://github.com/cduck/drawsvg>
Author: Casey Duckering
Author-email:
License:
Location: /home/sl/.local/lib/python3.10/site-packages
Requires:
Required-by:

I tried changing the shebang line and installing drawsvg everywhere.

Is the drawsvg module a .so or a .py?
Alleast with .py files the eXexcute permission makes no difference.

What exactly did your chmod +x do?
What does ls -l show for the drawsvg module?
Try python -v then import drawsvg to see where python looks for the module.

$ echo "print('module xyz')" >xyz.py
$ ll xyz.py
-rw-r--r--. 1 barry barry 20 2024-08-22 16:26:14 xyz.py
$ py3
Python 3.12.4 (main, Jun  7 2024, 00:00:00) [GCC 14.1.1 20240607 (Red Hat 14.1.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
:>>> import xyz
module xyz
:>>>

$ chmod +x xyz.py
$ ll xyz.py
-rwxr-xr-x. 1 barry barry 20 2024-08-22 16:26:14 xyz.py*
$ py3
Python 3.12.4 (main, Jun  7 2024, 00:00:00) [GCC 14.1.1 20240607 (Red Hat 14.1.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
:>>> import xyz
module xyz
:>>>

So, when you run the python script as python3 your-script.py, it’s executed by /home/sl/anaconda3/envs/project/bin/python3, but when you run it as ./your-script.py, it’s being executed by /usr/bin/python3. These are not the same even if you have installed drawsvg globally.

Try #! /usr/bin/env python3 and #! /home/sl/anaconda3/envs/project/bin/python3 instead, I bet one of them will work.


Never use an interpreter whose base name is python to run Python 3 programs. That name must be kept permanently reserved for Python 2.

1 Like

What is sys.path?

Related:

Likely tangential to your problem, but you generally want to avoid using which. In most shells, the path that’s returned by which isn’t necessarily what gets run when you type the same name at the command line. Prefer using whence, type -a, or command -v, depending on your use case and shell.

See your shell’s man page for which and this SE question for more details.

Just to this: try using:

 python -m pip show drawsvg

adjusting python to python3 or to the full path to the python
executable you’re using.

This is because pip is bound to a specific python and that might not
be the one you’re using. By invoking the pip module from a specific
python you get information about that pythons packages.