Creating and running python scripts

Creating files:

Use any text editor, as long as it can save into straight ASCII text.

Something with automatic indentation (e.g. a Python mode) like emacs or vi is suggested but not required.

Name your Python modules <valid_identifier>.py, e.g. 'file1.py' or 'file_1.py', but not 'file-1.py'. You have to be able to import them :)

Name your Python scripts any valid UNIX/Windows filename. You should avoid '/' and '\' as they are special characters.

Running files:

'python filename' should always work for valid Python. 'python -i filename' will run your code and then leave you at the interpreter prompt.

import filename

will work if 'filename.py' exists (and is valid Python, yada yada).

Code inside of 'if name == "main":' blocks will only execute when you run it with 'python filename' and not on import; this is useful if you want to be able to import a module without having it do something, but also want to be able to execute it from the command line as a script or a test. More on this later, with doctest and unittest.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License