Skip to main content

Python Tips

bytes and str conversion in Python

  • bytes -> str: b'abc'.decode() == ‘abc’
  • str -> bytes: 'abc'.encode() == b'abc'

shebang 1

  • ALWAYS use #!/usr/bin/env python3, which use the latest version of python3 installed
  • if there are multiple versions of python 3 installed, we can specify the version in shebang
  • #!/usr/bin/env python can refer to both python 2 & 3, should be avoid

Footnotes

  1. Should I put #! (shebang) in Python scripts, and what form should it take?, Zotero