Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing pyw files as modules under Linux

I have gotten some code from a colleague of mine written in Python for Windows. He is using several modules where files have the extension mymodule.pyw instead of mymodule.py. This works simply by doing

import mymodule

even if there is no file called mymodule.py. In Linux (Ubuntu 13.04 in my case) this does not work however. I get error messages of this form:

Traceback (most recent call last): File "main.pyw", line 27, in import core.main_window

ImportError: No module named main_window

simply renaming the module files to .py fixes the issue, but this is not desirable since I would like to use the same software on Windows and Linux.

Why is there this difference in the handling in Python and Windows and what can be done to fix it?

References: I seem to have found the original patch that added pyw support to windows, but with no argument as to why it shouldn't also apply to Linux.

like image 929
HansHarhoff Avatar asked Jan 27 '14 16:01

HansHarhoff


1 Answers

The pyw extension exists for the windows version of python, since windows draws a strong distinction between CLI and GUI applications. Separate extensions map to separate python interpreters, py to python.exe (CLI) and pyw to pythonw.exe (GUI).

This is only relevant for the purposes of double clicking on python files to launch the proper interpreter; thus the only file that should logically have the pyw extension is the 'entry point' script that needs to be double clickable; all other files, even on windows can and should have the py extension instead.

On linux, neither pyw nor py particularly help making scripts double clickable, it should instead bear a "shebang" on the first line, and have executable file permissions (chmod +x). On unix, executables conventionally have no extension at all, but if you prefer, can still end in .pyw, linux doesn't care one bit!

like image 143
SingleNegationElimination Avatar answered Oct 01 '22 13:10

SingleNegationElimination