Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check the syntax of Python code in Emacs without actually executing it?

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?

like image 413
Ray Avatar asked Oct 15 '08 17:10

Ray


People also ask

How do I check Python syntax without executing?

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 do you check Python syntax errors?

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.

How do you check code in Python?

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.


3 Answers

python -m py_compile script.py
like image 88
Matt Joiner Avatar answered Oct 16 '22 11:10

Matt Joiner


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

like image 25
Glyph Avatar answered Oct 16 '22 13:10

Glyph


Or from emacs (or vim) you could run python -c 'import x' where x is the name of your file minus the .py extension.

like image 2
mhawke Avatar answered Oct 16 '22 12:10

mhawke