English logga in

StartProgrammering

Python

Denna sida finns bara på engelska.

The programming language Python is used in programming courses and several projects here.

Version 2 or 3

The standard version (/usr/bin/python) in Centos 7 is 2.7, but our locally modified path is changed so that just python runs Python 3.6.

There are some incompatible changes in Python version 3, so that some programs can only be run in Python 2 or only in Python 3.

With python2 or python3 you can explicitly run one of the either. (If you want to to use the Idle environment it is started with idle2 or idle3 respectively. The latter is also found in the Applications menu. Also there is ipython2 as well as ipython3.)

Installed packages

Several packages are installed here, besides the standard Python packages. Some of them are listed here, but it is not a complete list. If you need to use some package here, first just try if it already is available!

textblob

TextBlob is a library for processing textual data which makes some tasks easier to do than most alternatives. See here!

NLTK

NLTK – Natural Language Toolkit – contains several program modules and corpuses for research and education in NLP and is installed here.

>>> import nltk
>>> tokens = nltk.word_tokenize("This is a test.")
>>> tokens
['This', 'is', 'a', 'test', '.']
>>> nltk.pos_tag(tokens)
[('This', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('test', 'NN'), ('.', '.')]
>>> 

There is an accompanying book, Natural Language Processing with Python, which starts as a beginner’s instruction in Python that uses language technology examples from the start.

spaCy

An NLP library. See Wikipedia and homepage.

Language models for English and French have been installed, and can be accessed with spacy.load('en') and spacy.load('fr') respectively.

Gensim

Vector space modeling and topic modeling toolkit. See Wikipedia and homepage.

Scikit-learn

Machine learning library. See Wikipedia and homepage.

Installing packages with pip

One way to install Python packages is with pip. Use pip2 or pip3 depending on whether it is for Python2 och Python3.

Note that pip in normal usage assumes that you are a system administrator and are going to do a system-wide installation for everyone. You can’t do that.

To instead do a personal installation for just you, use the option --user. Example:

$ pip3 install --user inflect
Collecting inflect
  Using cached inflect-2.1.0-py2.py3-none-any.whl
Installing collected packages: inflect
Successfully installed inflect-2.1.0
$ python
Python 3.6.6 (default, Aug 13 2018, 18:24:23)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inflect
>>> p = inflect.engine()
>>> p.plural('example')
'examples'
>>> p.plural('archnemesis')
'archnemeses'
>>>

Some packages will install programs for you to run. With a personal installation they will be in ~/.local/bin, and will be found in your PATH.

Checking code

pyflakes can find some errors in programs, like invalid syntax and undefined variables. It can be run with Python→Check File in the Emacs menus for a Python file.

pycodestyle checks if a Python program follows the style guide PEP 8.