Disclaimer
There are many other choices in every aspects of Python software development environment. New choices keep emerging in a speed that will always surprise us. It's even hard to define what are the 'typical' choices. Here we just list some examples and we'll try our best to include the 'best' ones that we know.
We respect all the choices.
And this doc will be updated frequently.
Here you can find some examples of a 'typical' Python development environment. Including:
You are recommended to read The History of Python.
At this moment Python 2.x and 3.x coexist as popular programming languages.
map
function.Depends on the target user, A good choice now quite often is to write code both work for Python 2.7 and 3.3/4. That usually means you need to configure your Continuous Integration System to use both interpreter.
There are several alternative Python implementations of the language interpreter.
Just type python
to enter into standard Python interactive mode.
!python
Python 2.7.5 (default, Aug 25 2013, 00:04:04) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
An alternative tool is IPython. It has more advanced features, like syntax highlight, code completion and integration with the OS (shell).
In the next section, we'll use IPython to show how to install additional Python packages.
The "standard" way of installing Python packages, is using the pip
command to install them from the Python Package Index(PyPI). PyPI is a repository of software for the Python programming language.
If you don't have pip
already, this is a good guide.
For example, to install IPython in your system globally:
# Don't do this, it's just a demo.
# You don't always want to install packages globally.
# Read on to know why.
!pip install ipython
Requirement already satisfied (use --upgrade to upgrade): ipython in /Users/terry/.virtualenvs/t1/lib/python2.7/site-packages Requirement already satisfied (use --upgrade to upgrade): gnureadline in /Users/terry/.virtualenvs/t1/lib/python2.7/site-packages (from ipython) Cleaning up...
OK, turned out I have ipython on my computer already.
You need to add sudo
in front of pip
on some operating system (when installing globally).
Adding new packages globally to your system will sooner or later become a problem. Packages and their versions might conflict with each other, and the Python version itself might also be a problem. This is especially a problematic when you are working on multiple projects at the same time. The required dependencies are difficult to satisfy if everything is installed globally.
That's why a virtual environment is almost mandatory if you are developing using Python. The most popular one so far is virtualenv. There is also a virtualenvwrapper, which makes working with virtual environments even easier. The link has a comprehensive guide on how to install it.
After installing it, you should be able to simply do:
mkvirtualenv ProjectA-env
to create a new virtual environment, and:
workon ProjectB-env
to switch to another virtual environment.
When working with virtual environments, every pip install
you execute will install packages inside the active virtual environment instead of the global location.
As a dynamic programming language, the IDE (Integrated Development Environment) or text editor for Python cannot do as much as they can for other programming languages like Java. So usually you can just use any text editor. Basically you are looking for three things:
Feature beyond that are often just nice to have.
Here we use (other than Vim) Eclipse with PyDev plugin (http://pydev.org/).
Install the latest version of Eclipse. From the Help menu select Install New Software:
Add "http://pydev.org/updates" in the "Work with" text box and select PyDev. Then follow the dialog to complete the installation.
As I'm creating these slides, PyDev 3.4 doesn't seem to work with Eclipse Kepler. The solution I found is to uncheck the checkbox "Show only the latest versions of available software." in the above dialog and install PyDev 2.8.2. I hope this bug will be fixed soon.
After the installation you will need to choose the Python Interpreter. If you are using global python environment, just click Auto Config here. If you are using a virtual environment, add New... and find the actual Python executable (for example at ~/.virtualenvs/virtualEnvName/bin/python
, but depends on where you created your virtual environment).
Now you can create a new PyDev project.
Why does Python get less support than Java from the IDE (or other editor)?
Should Python code completion feature be text-based or context-based?
Python's coding conventions are documented in pep-8.
The easy way to make sure you are following them is to install the pep8
package by:
pip install pep8
Python Enhancement Proposal (PEP) process is the primary mechanism for proposing major new features, for collecting community input on an issue, and for documenting the design decisions that have gone into Python. Outstanding PEPs are reviewed and commented upon by the Python community and by Van Rossum, the Python project's BDFL._ (from Wikipedia)
Then for code:
%%file example.py
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
Overwriting example.py
And then we can run:
!pep8 example.py
example.py:1:1: E302 expected 2 blank lines, found 0 example.py:3:5: E125 continuation line with same indent as next logical line
The most commonly used tool for Python code static analysis is pylint
(pip install pylint
to install it).
!pylint example.py
************* Module slides.example C: 4, 0: Final newline missing (missing-final-newline) C: 1, 0: Missing module docstring (missing-docstring) W: 2,13: Unused argument 'var_two' (unused-argument) W: 3, 4: Unused argument 'var_four' (unused-argument) W: 2,22: Unused argument 'var_three' (unused-argument)
There are many Python test frameworks. Here I just list what I am using:
unittest
module that comes with Python 2.7 and 3.x.mock
module that can be installed by pip install mock
, it's a standard module since Python 3.3.nose
, which can be installed by pip install nose
.Unfortunately (or luckily?), there is no Python's equivalent of Java's maven
, or Ruby's rake
. This is largely because Python modules themselves do not need any external intervention.
It's quite common for Python projects to use Makefile
to automate tasks such as the static checkers, unit tests and functional tests, and perhaps deployment.
Some new continuous integration system can do the same job as well.
We are still using Jenkins (yes, we do) as our CI server. And we use ShiningPanda Plugin so that it can use the virtual environment without polluting the whole CI server environment.
We also use a lot of travis-ci in our open source projects. It can be much more easier to separate the dependencies on the environment and it can easily be configured to test against multiple Python versions (including PyPy).
If you are interested in contributing:Open Sourcing a Python Project the Right Way
If you care about performance, everything is already included in the standard library: Python profiler