Sublime Text is an amazing editor for almost anything. The following article, by dbader.org, introduces the basic setting up for python users.
However, I am still not satisfied with the code "tip assist" plugin in Sublime.
For this point, I would recommend Wing IDE since this is the only one I tried with acceptable "tip assist" functionality.

Setting up Sublime Text for Python development

I recently started using Sublime Text 2 more and more as my main editor for Python development. This article explains my setup and some tweaks that make Python programmers happy.
My Sublime Text setup

Why Sublime Text?

I’ve been an avid user of TextMate for a long time. It’s light-weight, open-source, and as a native OS X application it feels very Mac-esque. While TextMate is a great editor, it seems very bare bones sometimes.
For some projects I used the more beefy IntelliJ IDEA with the Python plug-in. I especially like its debugger and test runner. Yet, often a full-blown IDE like IntelliJ is overkill when working on small to medium-sized projects.
In the last few weeks I began using Sublime Text more and more. Once I took the time to set it up I felt very much at home. It’s really fast, receives steady updates, and — as a big bonus — fully cross-platform. What finally won me over compared to TextMate was Sublime’s great plug-in ecosystem. There are several plug-ins available that make Python development very smooth and enjoyable.
I’m still switching editors on a per project basis now. But I noticed that for me Sublime Text seems to hit the sweet spot between a bare bones editor and a full-blown IDE for Python development.

Font choice

Ubuntu Mono is a great, great font. I’ve switched from primarily using Menlo a few days ago and I’m not regretting it so far.
With Ubuntu Mono, I find font size 16 very comfortable to read on my 15-inch MacBook. At 1680 × 1050 the sidebar plus two editor views (wrapped at 80 characters) fit nicely next to each other.
If you want to go nuclear on making the ideal font choice, this topic on slant.co gives a good overview. It includes screenshots and download links for the most popular programming fonts.

Installed plug-ins

As mentioned before, Sublime has a very extensive plug-in ecosystem. I’m currently using the following plug-ins:
  • Package Control A package manager for installing additional plug-ins directly from within Sublime.This should be the only package you have to install manually. All other packages listed here can be installed via Package Control. It’s also possible to update installed packages with Package Control. Simply think of it as the apt-get of Sublime packages.
  • Color Scheme - Tomorrow Night Color schemes determine the font colors used for syntax highlighting in the editor view. This is a very nice dark color scheme.
  • Theme - Soda Dark Themes change the color and style of Sublime’s UI elements. This one fits perfectly with the Tomorrow color scheme.
  • SideBarEnhancements This plug-in provides additional context menu options in the sidebar, such as “New file” or “New Folder”. These should be in there by default, but they are not.
  • All Autocomplete Sublime’s default autocomplete only considers words found in the current file. This plug-in extends the autocomplete word list to find matches in all open files.
  • SublimeCodeIntel Enhances autocomplete for some languages including Python. The plug-in also lets you jump to symbol definitions across files by pressing alt and then clicking on a symbol. Very handy.
  • SublimeREPL Allows you to run a Python interpreter session in an editor view. I tend to usebpython in a separate terminal window but sometimes SublimeREPL is helpful.
  • GitGutter Adds little icons to the editor’s gutter area indicating whether a line has been inserted, modified, or deleted according to Git. To get colored icons update your color scheme file as instructed in the GitGutter readme.
  • Pylinter This plug-in provides the best pylint editor integration I’ve seen so far. It automatically lints .py files whenever they’re saved and displays pylint violations directly in the editor view. It also has a convenient shortcut that locally disables a pylint check by inserting a #pylint: disablecomment. This plug-in really sealed the deal for me.

Preferences files

One of the nice things about Sublime Text is that it can be completely configured using simple JSON-based preferences files. This allows you to easily transfer your settings to another system. I’ve also seen people use Dropbox to automatically synchronize their settings on every computer they’re using.
Preferences.sublime-settings configures Sublime’s look-and-feel and its built-in behavior. You can open the file for editing within Sublime via Preferences > Settings — User. I’m using the following settings:
{
    // Colors
    "color_scheme": "Packages/Tomorrow Color Schemes/Tomorrow-Night.tmTheme",
    "theme": "Soda Dark.sublime-theme",

    // Font
    "font_face": "Ubuntu Mono",
    "font_size": 16.0,
    "font_options": ["subpixel_antialias", "no_bold"],
    "line_padding_bottom": 0,
    "line_padding_top": 0,

    // Cursor style - no blinking and slightly wider than default
    "caret_style": "solid",
    "wide_caret": true,

    // Editor view look-and-feel
    "draw_white_space": "all",
    "fold_buttons": false,
    "highlight_line": true,
    "auto_complete": false,
    "show_minimap": false,

    // Editor behavior
    "scroll_past_end": false,
    "highlight_modified_tabs": true,
    "find_selected_text": true,

    // Word wrapping - follow PEP 8 recommendations
    "rulers": [ 72, 79 ],
    "word_wrap": true,
    "wrap_width": 80,

    // Whitespace - no tabs, trimming, end files with \n
    "tab_size": 4,
    "translate_tabs_to_spaces": true,
    "trim_trailing_white_space_on_save": true,
    "ensure_newline_at_eof_on_save": true,

    // Sidebar - exclude distracting files and folders
    "file_exclude_patterns":
    [
        ".DS_Store",
        "*.pid",
        "*.pyc"
    ],
    "folder_exclude_patterns":
    [
        ".git",
        "__pycache__",
        "env",
        "env3"
    ]
}
Pylinter.sublime-settings configures the pylinter plug-in. I use the following settings to lint Python files automatically on save and to display graphical icons for lint violations:
{
    // Configure pylint's behavior
    "pylint_rc": "/Users/daniel/dev/pylintrc",

    // Show different icons for errors, warnings, etc.
    "use_icons": true,

    // Automatically run Pylinter when saving a Python document
    "run_on_save": true,

    // Don't hide pylint messages when moving the cursor
    "message_stay": true
}

Key bindings

Sublime’s key bindings are also fully user-configurable via JSON-based sublime-keymap preferences files. I’ve made a few changes to the default bindings to better serve my existing TextMate / IntelliJ muscle memory. You may not need to make changes to the key bindings at all. But if you want to, modifying them is very easy and transferable across platforms. I use the following additional key bindings:
[
    // Rebind "go to file" to cmd+shift+O
    { "keys": ["super+shift+o"], "command": "show_overlay", "args": {
        "overlay": "goto",
        "show_files": true
    }},

    // Rebind swap line up/down to cmd+shift+up/down
    { "keys": ["super+shift+up"], "command": "swap_line_up" },
    { "keys": ["super+shift+down"], "command": "swap_line_down" },

    // Delete a line with cmd+delete
    { "keys": ["super+backspace"], "command": "run_macro_file", "args": {
        "file": "Packages/Default/Delete Line.sublime-macro"
    }},

    // Reindent selection with cmd+alt+L
    { "keys": ["super+alt+l"], "command": "reindent"}
]

Command line tools

Similarly to TextMate’s mate, Sublime Text includes a command line tool that allows you to open the editor from the shell. The tool called subl is not enabled by default. To make it available from any shell do the following:
ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl
To use Sublime as the default editor for interactive Git commands, for example, when composing commit messages, add the following line to your ~/.profile:
export GIT_EDITOR="subl --wait --new-window"

Further inspiration

I hope this setup guide was helpful to you. If you’ve got any comments or suggested improvements, please feel free to drop me a line on Twitter or send an email. In addition, I’d like to thank the following authors for their articles on setting up Sublime. They inspired my setup and may give you additional inspiration for setting up Sublime Text as well:

Python libraries you should know

Posted by Jeffye | 1:03 PM

Pattern

Scrapy

NLTK


Coursera Downloader

Posted by Jeffye | 9:21 PM

Today I would like to introduce a python tool, Coursera downloader. Yes, you may already have your own ways to do this, but this python tool makes it much easier for you, especially for people who love python. Go and try it out. 
Coursera is arguably the leader in massive open online courses (MOOC) with a selection of more than 300 classes from 62 different institutions as of February 2013. Generous contributions by educators and institutions are making excellent education available to many who could not afford it otherwise. There are even non-profits with "feet on the ground" in remote areas of the world who are helping spread the wealth (see the feedback below from Tunapanda).
This script makes it easier to batch download lecture resources (e.g., videos, ppt, etc) for Coursera classes. Given one or more class names and account credentials, it obtains week and class names from the lectures page, and then downloads the related materials into appropriately named files and directories.
Why is this helpful? A utility like wget can work, but has the following limitations:
  1. Video names have a number in them, but this does not correspond to the actual order. Manually renaming them is a pain.
  2. Using names from the syllabus page provides more informative names.
  3. Using a wget in a for loop picks up extra videos which are not posted/linked, and these are sometimes duplicates.
DownloadThemAll is another possiblity, but this script provides more features such as appropriately named files.
This work was originally inspired in part by youtube-dl by which I've downloaded many other good videos such as those from Khan Academy.

What you have to install 

sudo apt-get install python-mysqldb //make python be able to connect to mysql server

wget https://www.djangoproject.com/download/1.5.1/tarball/

tar xzvf Django-1.5.1.tar.gz
cd Django-1.5.1
sudo python setup.py install

install mysql and apache2

sudo apt-get install libapache2-mod-wsgi //to host your python project

These are pretty much what you need to install.

Files you need to create 

In my case, create /home/ubuntu/firstweb.wsgi
import os
import sys

sys.path = ['/var/www/firstweb'] + sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'firstweb.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Another file you need to create is /etc/apache2/sites-available/firstweb

        WSGIScriptAlias / /home/ubuntu/firstweb.wsgi
        ServerAdmin webmaster@localhost
        ServerName  firstweb.com
        Alias /static /var/www/firstweb/static/


        
                Order allow,deny
                allow from all
        

        ErrorLog ${APACHE_LOG_DIR}/error.log


Note: remember to change the corresponding directory to suit your server. Here I list the full path of the files on my computer is only as a showcase such that you know where the files are.

Enable the sites and Restart Apach2 server

a2ensite /etc/apache2/sites-available/freeweb
/etc/init.d/apache2 restart

Having problems? 

if you have any problems to get Django runing, one quick way to debug it out is to check out your log file as follows:

less /var/log/apache2/error.log

Still cannot follow it?
watch the video and be patient since this is a 30+m long video.



Say, I host my website on Amazon ec2 server, and Amazon gave a public url: http://ec2-54-214-21-47.us-west-2.compute.amazonaws.com/drupal-7.22/

So how can I map my own domain name (say www.example.com ) to this website?

Just following the following steps:


  1. Create an Elastic IP on the AWS Panel, then associate it to your instance.  Note that when you turn off your instance for a long time, you need to re-associate your elastic ip to this instance. 
  2. Go to your domain management system (Mine is Goddady).  Add a A record to the DNS record pointing to the elastic IP address assigned in (1). Your domain provide should either give you some way to set the A record (the IP address), or it will give you a way to edit the nameservers of your domain.

If they do not allow you to set the A record directly, find a DNS management service like ZoneEdit, register your domain as a zone there and ZoneEdit will give you the nameservers to enter in the admin panel of your domain provider. You can then add the A record for the domain in ZoneEdit.
The basic service of ZoneEdit is free, you could also use Amazon route 53 or a similar pay-for service, if you preferred.
PS. 
If you want to host multiple website on your server, read 

Hosting multiple website with Apacha2

http://www.lleess.com/2013/05/hosting-multiple-website-with-apacha2.html

One of the most common Apache2 questions I've seen on Debian mailing lists is from users who wonder how to host multiple websites with a single server. 
This is very straightforward, especially with the additional tools the Debian package provides.

We've previously discussed some of the tools which are included in the Apache2 package, but what we didn't do was show they're used from start to finish.
There are many different ways you can configure Apache to host multiple sites, ranging from the simple to the complex. Here we're only going to cover the basics with the use of theNameVirtualHost directive. The advantage of this approach is that you don't need to hard-wire any IP addresses, and it will just worktm. The only thing you need is for your domain names to resolve to the IP address of your webserver.

For example if you have an Apache server running upon the IP address 192.168.1.1 and you wish to host the three sites example.comexample.net, and example.org you'll need to make sure that these names resolve to the IP address of your server.

(This might mean that you need example.com and www.example.com to resolve to the same address. However that is a choice you'll need to make for yourself).

Since we'll be hosting multiple websites on the same host it makes a lot of sense to be very clear on the location of each sites files upon the filesystem. The way I suggest you manage this is to create a completely seperate document root, cgi-bin directory, and logfile directory for each host. You can place these beneath the standard Debian prefix of /var/www or you may use a completely different root - I use /home/www.

If you've not already done create the directories to contain your content, etc, as follows:
root@irony:~# mkdir /home/www

root@irony:~# mkdir /home/www/www.example.com
root@irony:~# mkdir /home/www/www.example.com/htdocs
root@irony:~# mkdir /home/www/www.example.com/cgi-bin
root@irony:~# mkdir /home/www/www.example.com/logs

root@irony:~# mkdir /home/www/www.example.net 
root@irony:~# mkdir /home/www/www.example.net/htdocs
root@irony:~# mkdir /home/www/www.example.net/logs
root@irony:~# mkdir /home/www/www.example.net/cgi-bin

root@irony:~# mkdir /home/www/www.example.org 
root@irony:~# mkdir /home/www/www.example.org/htdocs
root@irony:~# mkdir /home/www/www.example.org/logs
root@irony:~# mkdir /home/www/www.example.org/cgi-bin
Here we've setup three different directory trees, one for each site. If you wanted to have identical content it might make sense to only create one, and then use symbolic links instead.
The next thing to do is to enable virtual hosts in your Apache configuration. Note: The simplest way to do this is to create a file called /etc/apache2/conf.d/virtual.conf and include the following content in it:
#
#  We're running multiple virtual hosts.
#
NameVirtualHost *
(When Apache starts up it reads the contents of all files included in /etc/apache2/conf.d, and files you create here won't get trashed on package upgrades.)
Once we've done this we can create the individual host configuration files. The Apache2 setup you'll find on Debian GNU/Linux includes two directories for locating your site configuration files:
/etc/apache2/sites-available
This contains configuration files for sites which are available but not necessarily enabled.
/etc/apache2/sites-enabled
This directory contains site files which are enabled.
As with the conf.d directory each configuration file in the sites-enabled directory is loaded when the server starts - whilst the files in sites-available are completely ignored.
You are expected to create your host configuration files in /etc/apache2/sites-available, then create a symbolic link to those files in the sites-enabled directory - this will cause them to be actually loaded/read.
Rather than actually messing around with symbolic links the Debian package includes two utility commands a2ensite and a2dissite which will do the necessary work for you as we will demonstrate shortly.
Lets start with a real example. 
Create /etc/apache2/sites-available/www.example.com with the following contents:
#
#  Example.com (/etc/apache2/sites-available/www.example.com)
#

        ServerAdmin webmaster@example.com
        ServerName  www.example.com
        ServerAlias example.com

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example.com/htdocs/

        # CGI Directory
        ScriptAlias /cgi-bin/ /home/www/www.example.com/cgi-bin/
        
                Options +ExecCGI
        


        # Logfiles
        ErrorLog  /home/www/www.example.com/logs/error.log
        CustomLog /home/www/www.example.com/logs/access.log combined

Next create the file www.example.net:
#
#  Example.net (/etc/apache2/sites-available/www.example.net)
#

        ServerAdmin webmaster@example.net
        ServerName  www.example.net
        ServerAlias example.net

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example.net/htdocs/

        # CGI Directory
        ScriptAlias /cgi-bin/ /home/www/www.example.net/cgi-bin/
        
                Options +ExecCGI
        


        # Logfiles
        ErrorLog  /home/www/www.example.net/logs/error.log
        CustomLog /home/www/www.example.net/logs/access.log combined


Finally create the file www.example.org:
#
#  Example.org (/etc/apache2/sites-available/www.example.org)
#

        ServerAdmin webmaster@example.org
        ServerName  www.example.org
        ServerAlias example.org

        # Indexes + Directory Root.
        DirectoryIndex index.html
        DocumentRoot /home/www/www.example.org/htdocs/

        # CGI Directory
        ScriptAlias /cgi-bin/ /home/www/www.example.org/cgi-bin/
        
                Options +ExecCGI
        


        # Logfiles
        ErrorLog  /home/www/www.example.org/logs/error.log
        CustomLog /home/www/www.example.org/logs/access.log combined

Now we've got:
  • Three directories which can be used to contain our content.
  • Three directories which can be used to contain our logfiles.
  • Three directories which can be used to contain our dynamic CGI scripts.
  • Three configuration files which are being ignored by Apache.
To enable the sites simply run:
root@irony:~# a2ensite www.example.com
Site www.example.com installed; run /etc/init.d/apache2 reload to enable.

root@irony:~# a2ensite www.example.net
Site www.example.net installed; run /etc/init.d/apache2 reload to enable.

root@irony:~# a2ensite www.example.org
Site www.example.org installed; run /etc/init.d/apache2 reload to enable.
This will now create the symbolic links so that /etc/apache2/sites-enabled/www.example.org, etc, now exist and will be read.
Once we've finished our setup we can restart, or reload, the webserver as the output above instructed us to do with:


root@irony:~# /etc/init.d/apache2 reload
Reloading web server config...done.
root@irony:~# 

How to create a virtual development environment by using virtualenv (say for python 3 on Ubuntu)


On Ubuntu 13.04, the default python is version 2.7. If you want to try out or really develop on top of python3, there would be lots of problems. Like for instance, when using pip to install packages, it is default to python2 packages. 

In these situations, virtualenv could be a good choice. 
You can find virtualenv on its official site http://www.virtualenv.org/en/latest/. As it is claimed , virtualenv is a tool for building independent python environment. The main goal of virtualenv is to solve the problems of dependence, version as well as indirectly permisions。
With virtualenv, your python2 would not interfere with python3 environment.

Step-by-step: 

First, install virtualenv: 
1$ [sudo] pip install virtualenv
create virtual environment:


1$ virtualenv -p /usr/bin/python3 py3env

activate this virtual environment:

source py3env/bin/activate
Now, you have probably noticed that there is a "py3env".

Then, you can download some third part libs: 
1pip install httplib2

exit the python3 virtual environment
1$ deactivate
that's it.


If you do not have root privilege, please refer to

How to install python modules without root access?

How to install python modules without root access?

You may have been given an 'instructional account',  which is a school account or shared account that you can ssh into to do work. However, you may also want to run some computationally intensive Numpy, matplotlib, scipy code on that machine. Unfortunately, you cannot install these modules because you am not a system administrator.

How can I do the installation?

With easy_install you can do:

easy_install --prefix=$HOME/local package_name
which will install into $HOME/local/lib/pythonX.Y/site-packages (the 'local' folder is a typical name many people use, but of course you may specify any folder you have permissions to write into).
You will need to manually create $HOME/local/lib/pythonX.Y/site-packages and add it to your PYTHONPATH environment variable (otherwise easy_install will complain -- btw run the command above once to find the correct value for X.Y).
If you are not using easy_install, look for a prefix option, most install scripts let you specify one.

With pip you can use:

pip install --install-option="--prefix=$HOME/local" package_name
or, even better (from comment below), you can rely on the so-called "user site" location (see the PEPfor details) by doing
pip install --user package_name

(a-virtualenv)me@pc:~$ ipython
Traceback (most recent call last):
  File "/usr/bin/ipython", line 19, in 
    from IPython.frontend.terminal.ipapp import launch_new_instance
ImportError: No module named IPython.frontend.terminal.ipapp

This problem may happen to you for some reason. To work around this, I add this to my /usr/bin/ipython

import sys
if "/usr/lib/python2.7/dist-packages" not in sys.path:
    sys.path.append("/usr/lib/python2.7/dist-packages")

This trick works for Balthazar.






Popular Posts