parm530
10/10/2019 - 3:22 PM

Python Tips

Current Version of Python

  • python3 --version -> Python 3.7.4
  • Alternatively try python --version
  • If no versions appear:
    • Link to install python (might be best to install it)
    • Click on Downloads -> Download Python version_number
    • Once installed, open Python folder and click to run Install Certificates.command

Installing Visual Studio Code

  • Install here
  • Open and click on Extensions icon, upper left column, last icon
  • Search for Python, created by Microsoft and then install
  • Restart VSCode
  • If you open a directory that contains python files and look at the bottom left part of the editor, you should see a python version. If not:
    • Command + Shift + p hold down all three and when the search bar appears type in python: select interpreter, then select it and choose the version of python you have installed.
  • Now, when you click the debug icon on the left pane, and confirm No Configurations, click on the gear:
    • You should be able to select Python as your environment (help)
    • It should then open a launch.json file
      • Add "cwd": "" to the json list
    • Additionally, if you have both python 2 and python 3 installed, then you might want to tweak the python path by:
      • Code -> Preferences -> Settings
      • Search for python.pythonPath and confirm that it uses python3

Python Interpreter Mode

  • Open terminal
  • run python3 to enter python command line mode
  • exit() to exit the interpreter

Run Python Files

  • python3 path_to_file

Python __name__

  • Distinguishes if the file is being called directly or if the file was being included
    • If being called directly, python assigns the __name__ variable to __main__

Python Naming Conventions

  • packages and modules: lower case separated by underscores (parms_module)
  • classes should be upcased with no underscores (ParmsClass)
  • global variables lower case with underscores
  • instance variables lower case with underscore
  • method names lower case with underscore
    • method args should have 1st argument named self
    • class methods should have 1st argument named cls
  • function names should be lower case with underscore
  • constants are all caps with underscore (MAX_VALUE)