Python's IDLE has 'Check Module' (Alt-X) to check the syntax which can be called without needing to run the code. Is there an equivalent way to do this in Emacs instead of running and executing the code?
Use the shell command python -m py_compile path/to/file , where path/to/file is the path to a Python script to compile the script without executing it. If there was an error in the compiling, it would be printed to the terminal. The code in the file will not run.
How to check the syntax of your Python code: First, Drag and drop your Python file or copy / paste your Python text directly into the editor above. Finally, you must click on "Check Python syntax" button to start code checking.
Practical Data Science using Python We use the getsource() method of inspect module to get the source code of the function. Returns the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string.
python -m py_compile script.py
You can use Pyflakes together with Flymake in order to get instant notification when your python code is valid (and avoids a few common pitfalls as well).
Or from emacs (or vim) you could run python -c 'import x'
where x is the name of your file minus the .py
extension.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With