An awesome piece of hackery, a Python interpreter in less than 100 lines of code.
This interpreter is fairly full-featured. It supports:
- Executing Python source files
- Executing Python module directories (following the same rules as the Python 2.7 interpreter)
- Executing ZIPed Python modules
- Provides an enhanced interactive interpreter (cross-session history is the only enhancement, but still...)
- Supports
-m
to execute a module on path - Supports
-c
to execute a Python command - Supports
-i
to drop into a python interpreter after executing a script or module - Supports a
site-packages
directory (must be in the same directory as the script) - Supports relative and absolute imports
- Supports Python source being piped in on stdin
- Entire standard library is now included
Planned
These features are planned (but may never be implemented):
- Put out releases packaged with pyinstaller into a single executable
- Include a few really useful packages like (and all of these might not be possible):
- paramiko
- pandas
- openpyxl
- IPython
- PyInstaller
- pip
- pexpect
- more to come as I think of them
- provide a place to put executable scripts (like
bin
orscripts
) - If you can think of anything else you'd like to see, please open an issue on GitHub or better yet a Pull Request
Now that we have an official beta release, you can head over to the downloads and grab a copy.
There is no installation required. There is, however, one thing you should know about the binary releases, there are two files for each platform (except win32 because I don't actually have access to a 32 bit windows machine at the moment), a zip file and an executable file.
The executable file is ready to go, just grab it (and change the permissions if you have to) and run it. It is a full featured, single file Python distribution ready to go, but upon every execution it unzips itself into a temporary directory. This adds overhead, so if that's a problem grab the zip file.
The zip file can be extracted anywhere and it will make a directory called ./moonpy/
which contains everything you need to run. This only needs to be unzipped once and it can be executed with no additional overhead.
Building is quite simple, but first you have to have the pre-requisites installed:
- python
- PyInstaller
- (windows only)pyreadline
NOTE: The tests will only run on Linux for right now. The offending tests
are testing the ability to run scripts. They make a NamedTemporaryFile
,
write code to them and point both moonpy and python at them. Windows does not
support having a file opened multiple times (at least I believe this is the
case I haven't actually tested this, but if things change please open an issue)
- pexpect
- py.test
to install these dependencies, issue the following commnd:
$ pip install pexpect
$ pip install py.test
I built this using Python 2.7, but I'd be very interested to hear how this works on other versions
The easiest way to install Python is from The Official Site
This used to "build" (rather bundle) the single-file executable
The easiest way to install PyInstaller is with pip
$ pip install pyinstaller
This is used to support command history in the interactive console and to persist that history between sessions. This package is needed for Windows because they do not include GNU readline and so the Python readline module is not available for users within the Standard Library.
The easiest way to install pyreadline is to use pip
:
C:\> pip install pyreadline
Before building it would be a good idea to run the tests. If you've installed the Additional pre-requisites for running tests then you simply need to navigate to the directory and issue the following command:
py.test
Once The pre-requisites are installed, you simply need to issue the following command:
$ pyinstaller --onefile moonpy.py
This will give you a base executable, which is a fairly-fully featured Python interpreter, but it is missing most of the standard library. To get this you will need to copy the packages manually over to where the executable is located. For convenience's sake, if a directory named site-packages
exists in the same directory as the executable, it will be added to the path (as well as the current directory).
This file should behave exactly like python when the supported features are used. I mean to say, the features I've chosen to support should behave exactly like the official CPython executable (see features for the list of supported features). If moonpy does not act like CPython, then it is a bug so please report it to the Github issue tracker or issue a pull request.
If you wish to contribute, please feel free to submit issues or issue pull-requests. The coding-stlye here is:
Try to be pep8 compliant, but I REALLY want to keep this under 100 lines of code. Also every change should have an associated test.
Thanks for taking the time to stop by. Happy coding!