Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Tips
#1
I have been programming in Python since 2006 (with breaks) from version 2.5. This would be my recommended programming language for people who want to learn programming. There is Python library for everything under the sun, it is general purpose programming language and pre-installed on almost any computer including Raspberry Pi, It's perfectly suitable for tooling, generally it is several times faster to write a utility in Python than C++ or Java, dont get me started on Bash.

This is not supposed to be a tutorial, you can find plenty of those online, more like my random notes.

Python homepage is here: https://www.python.org/

PIP

Next thing I always install is PIP, install it like this:

Code:
curl https://bootstrap.pypa.io/get-pip.py | python

Then you can install Python packages like this:

Code:
pip install some-package-name

If that does not work, this always works:

Code:
python -m pip install some-package-name

SimpleHTTPServer

The -m option allows to run python library module as Python script. I frequently use it also like this:

Code:
python -m SimpleHTTPServer

This will start webserver on port 8000 with directory indexing capability, so you can download files from your computer using wget or curl.

Shebang

If you include shebang on the first line and make your script executable (chmod +x myscript.py), it can be called like any other command.

Code:
#!/usr/bin/env python
Btw. on Windows it's possible to call .py files from command line without prepending "python"

Editor

Most of the time you do not need full IDE, just an editor. Some people use whatever basic editor is included and have command line open where they test it. This is too crude, I prefer editor with line numbers that allows running the script captures output and errors and allows navigation in the source by double-clicking on error line. The one I have used since ever is SciTE. On Linux install

Code:
sudo apt-get install scite

That's all for now
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)