1

I have a Python program, and I want to detect whether it was compiled with mypyc or not so I can include this in the version information of the program for debugging purposes. But how can I do this? I tried looking through dev-intro.md in the mypyc docs, but I couldn't find anything useful. I also looked through Differences from Python in the mypy docs, but I couldn't find anything there either.

How can I detect whether a Python module was compiled with mypyc?

2 Answers 2

1

I ended up copying the way mypy and mypyc do it, which is just to check whether __file__ ends with ".py" (link to relevant code).

A sample implementation could be done like this:

def is_mypyc_compiled() -> bool: return not __file__.endswith(".py") 

This works for my use-case, but may not work if you're doing something unconventional with your Python source code filenames.

Sign up to request clarification or add additional context in comments.

Comments

0

You can implement a function which will check if there are mypyc attributes in the module.

For example:

def check_mypyc(module): return hasattr(module, '__mypyc_attrs__') 

3 Comments

I just tested this, and it doesn't seem like the __mypyc_attrs__ attribute exists for me. I'm using sys.modules[__name__] to get the current module. I also attempted to verify that it's not using the Python source code by moving the source file somewhere else, and the code still ran without issues, so it does seem like it's using the mypyc binary. Additionally, I did dir(sys.modules[__name__]) and printed this — no mention of __mypyc_attrs__. Do you have any ideas? This is with mypyc 1.12.0.
Side note: Did you get this answer from some AI?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.