Python scripts are ran in ImageMagick instead of Python

Hello, I come from the Linux Mint Forums.*
I am unable to run Python scripts normally because when I do, instead of Python interpreting code, ImageMagick does it’s magick with the Python script instead.
I think ImageMagick is involved since importing libraries makes ImageMagick take a drag-and-drop screenshot like Microsoft Snip and Sketch.
I’ve ran out of help there. Any help here?

*Link to my post on LMF

… Ok wow, that is quite strange behavior.

To verify that I understand correctly:

If you have a file containing import pygame, potentially followed by some more python code and then execute it with python3 while the venv is active, instead ImageMagick takes a screenshot and stores it with the name pygame? And you don’t see any other console output?

If yes:

  • does this happen if there is code above import pygame? If yes, does this code get executed as python code? (try with e.g. a simple print statement. The short snippet shared by the other user printing the module list might also be interesting)
  • Can you try running with -v to see which imports happen?

When this happens to me, it’s because I made the script executable (with chmod +x) but forgot to add the shebang line. That results in the script being run as a shell script, which interprets the import statements as calls to the ImageMagick import command.

Running the script by explicitly invoking python3 on it (e.g. python3 ./script.py) should still work in this case. It’s just running it directly (e.g. ./script.py) that doesn’t work.

Anyway, try adding the shebang line as the very first line of your script and see if that fixes it:

#!/usr/bin/env python3

import os
print("Hello world")
3 Likes

That is explicitly the scenario they are reporting which makes this very strange.

For clarification, does ImageMagick hijack (1) all python scripts, (2) any python script with import <something> on the first line, or (3) only python scripts with import pygame on the first line?

You could try to strace the system calls that occur when you run a python script. Run strace python <path/to/script.py> and post the output. It will be quite verbose.

If a text file has no #! the shell is used to run the code.
This is expected behaviour on unix system.

Nothing looks at the file type before running the script in bash.

Add an explicit shebang and everything will work as required.

So you are calling OP a liar and that what they are saying they are doing (python3 script.py) is not actually what they are doing?

I was replying to the imagemagic hijack comment.
I have no opinion on the OPs situation, not enough details have been shared, which is leading to other people guessing at the possible causes.

If you follow the link, a lot of information has been shared; I am sure OP is willing to provide more information if you have questions.

But I am also noticing many people here not even trying to understand the situation.

python script.py opening ImageMagick is not expected behavior in unix, with or without shebang.

Agreed. It seems that mint forum are debugging that for the OP.

Man, please actually read stuff instead of assuming. That discussion ends with:

Honestly, I’m out of ideas. I suggest trying the official Python forum at https://discuss.python.org/ .

If you don’t want to discuss this here, then don’t participate. But all you have done in this thread till now is distract from the actual topic and prevent others from helping.

So. It appears I’m supposed to use Shebangs.

For context, I’m a new-ish Linux user. Using Windows until late December. Never heard of Shebangs in my life before now.

Although, I tried using the Shebang Henrik gave me, but instead of using /usr/bin/env, which doesn’t exist on my computer, I just used the path to Python in my venv.
Yet, the terminal just gives me a permission error :​/

Also… I may have been running o.py from Nemo*… I’ve been trying to make my Linux experience less centered around using the terminal just like in Windows^… so… uh…

*Judging from (a) comment(s) on the Linux Mint Forum, I might need to specify that I’m doing this by double clicking, then clicking Run in Terminal

^Not to be confused with outright avoiding the terminal, I’ve ran commands in Command Prompt before

The shebang may help, but I also recommend reading up a bit because this is a feature of posix systems which you will probably find useful in your Linux adventures. Shebang lines declare what program should be used to run a script.

Not having env installed at that location is unusual for a Linux system (although not against any strict rules). Are you absolutely certain it’s not there? I’d check stat /usr/bin/env to be sure.

It might also help if you share whatever shebang line you did put in place. Either we can confirm for you that it looks right, or possibly spot an error.

We should first figure out your issue in the terminal context, and it would be best to avoid using a GUI launcher until we’ve made more progress helping you troubleshoot.
The Nemo GUI is a source of uncertainty and variation we can eliminate.

Open a terminal, cd to the directory with your script, activate your virtualenv, and run python my-script.py. If that works, we’ll have learned a great deal, and from that place of knowledge we can try to help you get your desired GUI workflow to work.

(As a separate matter, I am a proponent of getting comfortable in the shell. I think you will enjoy and appreciate Linux more if you do. But let’s not force that on you here and now!)

It’s hard for me to be certain from that screenshot, but I would guess that you haven’t set the execute bit on your script.

I recommend not worrying about this yet, and starting with the explicit python invocation I suggest above – as long as the file is readable, that should work.
And if it doesn’t work, it will probably print out a textual error message which you can paste back here for us to see.


EDIT: I want to clarify that I know I’m asking for us to try steps recommended in the Mint forums again. I feel that’s the best way to start in case some detail got mixed up. The report is simply too strange for me not to consider it worth our while to check again.

1 Like

I mistook /usr/bin/env for a folder :grimacing:

It exists and works fine.

using python3 o.py or ./o.py or Run in Terminal (Nemo)* works perfectly fine now

*Run should work, but I haven’t made GUI :person_shrugging: