# Python scripts are ran in ImageMagick instead of Python

**URL:** <https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250>\
**Category:** Python Help\
**Created:** [April 22, 2025, 8:51pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250 "2025-04-22T20:51:01Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![mint\_user\_9998](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mint_user_9998/32/27422_2.png) [@mint\_user\_9998](https://discuss.python.org/u/mint_user_9998)\
**Post date:** [April 22, 2025, 8:51pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/1 "2025-04-22T20:51:02Z")

</div>

Hello, I come from the [Linux Mint Forums](https://forums.linuxmint.com/viewtopic.php?p=2603072).\*  
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

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [April 22, 2025, 9:18pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/2 "2025-04-22T21:18:05Z")

</div>

… 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?

---

<div class="post-metadata">

**Author:** ![tunedal](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tunedal/32/14639_2.png) [@tunedal](https://discuss.python.org/u/tunedal)\
**Post date:** [April 22, 2025, 9:45pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/3 "2025-04-22T21:45:29Z")

</div>

When this happens to me, it’s because I made the script executable (with `chmod +x`) but forgot to add the [shebang line](https://docs.python.org/3/using/unix.html#miscellaneous). 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:

```python
#!/usr/bin/env python3

import os
print("Hello world")

```

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [April 22, 2025, 10:53pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/4 "2025-04-22T22:53:11Z")

</div>

> [@tunedal](#):
>
> Running the script by explicitly invoking `python3` on it (e.g. `python3 ./script.py`) should still work in this case.

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

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [April 23, 2025, 6:55am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/5 "2025-04-23T06:55:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [April 23, 2025, 7:18am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/6 "2025-04-23T07:18:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [April 23, 2025, 7:44am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/7 "2025-04-23T07:44:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [April 23, 2025, 7:49am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/8 "2025-04-23T07:49:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [April 23, 2025, 7:59am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/9 "2025-04-23T07:59:26Z")

</div>

> [@barry-scott](#):
>
> not enough details have been shared,

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.

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [April 23, 2025, 9:14am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/10 "2025-04-23T09:14:05Z")

</div>

> [@barry-scott](#):
>
> This is expected behaviour on unix system.

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

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [April 23, 2025, 10:50am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/11 "2025-04-23T10:50:29Z")

</div>

> [@abessman](#):
>
> `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.

---

<div class="post-metadata">

**Author:** ![MegaIng](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/megaing/32/16162_2.png) [@MegaIng](https://discuss.python.org/u/MegaIng)\
**Post date:** [April 23, 2025, 11:15am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/12 "2025-04-23T11:15:09Z")

</div>

> [@barry-scott](#):
>
> 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/](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.

---

<div class="post-metadata">

**Author:** ![mint\_user\_9998](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mint_user_9998/32/27422_2.png) [@mint\_user\_9998](https://discuss.python.org/u/mint_user_9998)\
**Post date:** [April 23, 2025, 10:11pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/13 "2025-04-23T22:11:02Z")

</div>

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 :​/

 ![Screenshot of Gnome-Terminal](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/c/b/cb455f8fa0326fc440be1092ce792c428865b85f.png)

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

---

<div class="post-metadata">

**Author:** ![sirosen](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/sirosen/32/8478_2.png) [@sirosen](https://discuss.python.org/u/sirosen)\
**Post date:** [April 24, 2025, 5:30am UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/14 "2025-04-24T05:30:31Z")

</div>

> [@mint\_user\_9998](#):
>
> 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.

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.

> [@mint\_user\_9998](#):
>
> 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.

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.

> [@mint\_user\_9998](#):
>
> 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_

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!)

> [@mint\_user\_9998](#):
>
> Yet, the terminal just gives me a permission error :​/

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.

---

<div class="post-metadata">

**Author:** ![mint\_user\_9998](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mint_user_9998/32/27422_2.png) [@mint\_user\_9998](https://discuss.python.org/u/mint_user_9998)\
**Post date:** [May 1, 2025, 8:00pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/15 "2025-05-01T20:00:09Z")

</div>

I mistook `/usr/bin/env` for a folder 😬

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 🤷

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [September 4, 2025, 4:38pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/16 "2025-09-04T16:38:30Z")

</div>

Split this topic on September 4, 2025, 4:38pm UTC

A post was split to a new topic: [`import sys` runs ImageMagick](https://discuss.python.org/t/import-sys-runs-imagemagick/103547)

---

<div class="post-metadata">

**Author:** ![noemirtil](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/noemirtil/32/30606_2.png) [@noemirtil](https://discuss.python.org/u/noemirtil)\
**Post date:** [September 4, 2025, 4:39pm UTC](https://discuss.python.org/t/python-scripts-are-ran-in-imagemagick-instead-of-python/89250/17 "2025-09-04T16:39:24Z")

</div>

Thanks for splitting!
