I recently had to get my Windows 7 laptop ready for some Python development. I thought it would be a good idea to write a note about what I did to quickly set up a development environment for Python on Windows.
Install python
Download the installer for the version of Python you want to install from https://www.python.org/. I installed Python 3.6.1 which is the latest version at the time of writing.
Install virtualenv
So, why do we need virtualenv? Well, virtualenv allows you to create isolated environments for your Python projects so that you do not accidentally end up breaking your code by installing conflicting dependencies for other projects .
This article (https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/) gives an excellent explanation of why we need virtualenv.
pip, the package manager for python comes installed with python 3.6. Use pip to install virtualenv.
1 |
pip install virtualenv |
Configure SublimeText
I already had Sublime Text installed on my laptop along with Package Control and a theme of my choice (I like Material Theme). So all I had to do was modify some settings and install some plugins which would allow me to code efficiently in Python. If you don’t have Sublime Text you can download it from https://www.sublimetext.com/3
Handling Tabs
A very important text editor setting so far as Python is concerned is how tabs are handled. I use the following settings in my user preferences (Preferences->Settings in the SublimeText menu). This ensures that any time I hit a tab key, the single ‘tab’ character is replaced by four ‘space’ characters.
1 2 |
"tab_size": 4, "translate_tabs_to_spaces": true, |
Setting up Code Auto Completion
Code Auto Completion is a must-have feature for any code editor. The SublimeJedi plugin allows you to set up code auto completion for Python in Sublime Text. After installing the SublimeJedi plugin, you can configure it by adding the following to your Sublime Text settings. I usually do this in my project specific settings, setting the python interpreter path to the python executable for my virtual environment for that particular project.
1 2 3 4 5 6 7 |
"settings": { // ... "python_interpreter_path": "<path to python executable>", "python_package_paths":[ "<additional path to be searched for python packages containing symbols>" ] } |
This is what my ‘python-tests.sublime-project’ file looks like, ‘python-tests’ being the name of my sublime project. I like to set up a virtualenv for all my projects, so the path to my python executable points to python inside my virtualenv.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
{ "build_systems": [ { "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "name": "Python35", "selector": "source.python", "shell_cmd": "\"python\" -u \"$file\"" } ], "folders": [ { "path": "D:\\Workspace\\pythontests" } ], "settings": { "python_interpreter_path": "D:\\Workspace\\python-envs\\pytestenv\\Scripts\\python", "python_package_paths": [ "D:\\Workspace\\python-envs\\pytestenv\\Lib", "D:\\Workspace\\python-envs\\pytestenv\\Lib\\site-packages" ] } } |
Setting up Code Linting in Sublime Text
For code linting , I installed the python linting package ‘flake8’ into my virtual env. flake8 flags style as well as syntactic violations.
1 2 3 |
D:\Workspace>python-envs\pytestenv\Scripts\activate (pytestenv) D:\Workspace>pip install flake8 |
Then I installed SublimeLinter and SublimeLinter-flake8 using Package Control and changed my settings (at Preferences -> Package Settings -> SublimeLinter -> Settings – User )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
{ "user": { "debug": false, "delay": 0.25, "error_color": "D02000", "gutter_theme": "Packages/SublimeLinter/gutter-themes/Blueberry/cross/Blueberry - cross.gutter-theme", "gutter_theme_excludes": [], "lint_mode": "background", "linters": { "flake8": { "@disable": false, "args": [], "builtins": "", "excludes": [], "executable": "", "ignore": "", "jobs": "1", "max-complexity": 10, "max-line-length": null, "select": "", "show-code": false } }, "mark_style": "squiggly underline", "no_column_highlights_line": true, "passive_warnings": false, "paths": { "linux": [], "osx": [], "windows": [ "D:\\Workspace\\python-envs\\pytestenv\\Scripts" ] }, "python_paths": { "linux": [], "osx": [], "windows": [ "D:\\Workspace\\python-envs\\pytestenv\\Scripts" ] }, "rc_search_limit": 3, "shell_timeout": 10, "show_errors_on_save": false, "show_marks_in_minimap": true, "syntax_map": { "html (django)": "html", "html (rails)": "html", "html 5": "html", "javascript (babel)": "javascript", "magicpython": "python", "php": "html", "python django": "python", "pythonimproved": "python" }, "tooltip_fontsize": "1rem", "tooltip_theme": "Packages/SublimeLinter/tooltip-themes/Default/Default.tooltip-theme", "tooltip_theme_excludes": [], "tooltips": false, "warning_color": "DDB700", "wrap_find": true } } |
Follow Along during Debugging
The PDBSublimeTextSupport plugin shows you the line of code you are on when you debug your python code using pdb. When you start a debugging session with pdb and the debugger hits a breakpoint , the line is highlighted automatically in your SublimeText editor. I installed PDBSublimeTextSupport into my virtual env using the following command.
1 2 3 |
D:\Workspace>python-envs\pytestenv\Scripts\activate (pytestenv) D:\Workspace>pip install flake8 |
Formerly a Senior Manager at Capgemini and currently Jack of all Trades at Brandsoft, Sanjucta has 17 years of experience on various domains, platforms and technologies. She still finds it exhilarating to acquaint with the new kids in the block, be it technology or human resource.