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__