# Python development environment (with ROS!) # Install ipython ![ipython screenshot](http://i.imgur.com/qEXjcLE.png) A powerful and pretty interactive shell. sudo apt-get install ipython Get started at the [official documentation](http://ipython.readthedocs.org/en/stable/interactive/tutorial.html#the-four-most-helpful-commands). # Install sublime text 3 ![sublime text 3 screenshot](http://www.sublimetext.com/screenshots/new_theme_large.png) sudo add-apt-repository ppa:webupd8team/sublime-text-3 sudo apt-get update sudo apt-get install sublime-text-installer ## To disable new version notifications: Open sublime with subl Add the following to your User Preferences file (Preferences > Settings - User): "update_check": false ## To comment blocks of code Shortcuts by default are for english keyboard, for spanish keyboard you probably want Control+7/Control+shift+7 to work on comment blocks of code. Add the following to your User Preferences file (Preferences > Key bindings - User): { "keys": ["ctrl+7"], "command": "toggle_comment", "args": { "block": false } }, { "keys": ["ctrl+shift+7"], "command": "toggle_comment", "args": { "block": true } }, In order to change any key binding you can check what is the default (Preferences > Key bindings - Default) and just copy in the user prefences file with the modified keys. To know which key combination you are pressing you can go to "View->Console" and type sublime.log_input(True) ## Translate tabs to spaces by default You can always change it from the bottom right menu for any file, but by default we would to always use spaces when using ROS files. Add the following to your User Preferences file (Preferences > Settings - User): "translate_tabs_to_spaces": false, ## Install Anaconda Python IDE ![anaconda gif](http://damnwidget.github.io/anaconda/img/screenshots/animations/mccabe.gif) [Anaconda](http://damnwidget.github.io/anaconda/) sublime text plugin, not to be confused with [Anaconda from Continuum analytics](https://www.continuum.io/why-anaconda), is a plugin that turns your SublimeText 3 into a rich featured Python development stack that boost your productivity and helps you to ensure the quality and style of your code. To install it follow the [installation instructions](http://damnwidget.github.io/anaconda/#using-anaconda-installation), resumed here as: ### Install Package Control Go to View > Show Console and paste: ```` import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by) ```` ### Install Anaconda plugin trough package control Go to Tools > Command Palette (or Control+Shift+P) and write `install package` then write `anaconda` in the input window popup window and press enter. After restarting SublimeText it will be able to auto complete (on tab, on dot), show documentation (control+alt+d), go to the declaration of stuff (control+alt+g), autoformat (control+alt+r), find usages (control+alt+f). The shortcut keys can be changed by going to Preferences > Package Settings > Anaconda > Key bindings, use the default to know what you can change and write it in the user one like we did with the keybindings before. If you want a behaviour similar to QtCreator or Eclipse on going to declaration by Control+Click, you can do: ```bash echo '[ { "button": "button1", "count": 1, "modifiers": ["ctrl", "alt"], "press_command": "drag_select", "command": "anaconda_goto" } ]' > ~/.config/sublime-text-3/Packages/User/Default\ \(Linux\).sublime-mousemap ``` Which will enable Control+Alt+click to go to the declaration. (Control+Click gives you multiple cursors in SublimeText). You'll get also linting for the code and other goodies, check all it can do at the documentation page. Note that you must open it from the shell with a sourced environment (source myworkspace/devel/setup.bash) to get autocompletion and such. ### Ignore a PEP8 on a single line If you ever want to ignore the PEP8 on a line just add at the end of it: '` # NOQA`' note that there are two spaces before the `#` and one after. Useful for comment lines with big URL's for example. ## Bonuses of Sublime Text You can not only use Sublime Text for dealing with Python code, but also... ### Open launch files with highlighting Just open a .launch file and click on the bottom right of the window corner (it says the current highlighting provided) and click on "Open all current extension as..." and choose XML. ### Edit and Preview MarkDown files Control+Shift+P write `install` and write markdown, choose MarkdownEditing. Control+Shift+P write `install` and write markdown, choose MarkDown Preview. Choose (bottom right corner) MarkdownEditing > Markdown / Markdown GFM from the file syntax to use it. To ease preview in browser add to Preferences > Key bindings - User: { "keys": ["alt+m"], "command": "markdown_preview", "args": {"target": "browser", "parser":"markdown"} }, To preview doing alt+m. # Install PyCharm ![Pycharm logo](http://i.imgur.com/KORsckV.png) Even tho SublimeText is great, it may lack some maturity that PyCharm may have. For example, it helps you auto-fill documentation strings, convert methods to properties, hints that you can convert something to a static method, hints you to specify return type... etc. The downside is a slower starting time and a Eclipse-like feeling (if that is a downside for you). ## Install java8 PyCharm is like a heavily modified Eclipse, so it's based in Java. sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer ## Actually install PyCharm (Community edition) I find it easier to install it from a repository: wget -q -O - http://archive.getdeb.net/getdeb-archive.key | sudo apt-key add - sudo sh -c 'echo "deb http://archive.getdeb.net/ubuntu trusty-getdeb apps" >> /etc/apt/sources.list.d/getdeb.list' sudo apt-get update sudo apt-get install pycharm You can now just type pycharm to open it. **Note that you must open it from the shell with a sourced environment (source myworkspace/devel/setup.bash) to get autocompletion and such.** I choose the Darcula IDE theme (it's darker) usually, up to the user. ## Editor to have the same color theme as the app itself Go to File > Settings > Editor > Colors & Fonts and choose your Scheme (Darcula for me). Hit Apply. # ROS package with python As you may know ROS packages can contain Python code. It is structured in the following manner: ## scripts In the `scripts` folder, as the name implies, you'll place the scripts you create. Either the final executable of some awesome library you created or just plain self contained scripts. **You cannot import from somewhere else these files.** In order to install these scripts (so they are deployed) you need to add to `CMakeLists.txt` these executables like: # Install scripts install(PROGRAMS scripts/my_awesome_script.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) ## nodes Some ROS wiki page recommends to put nodes in a `nodes` folder, everything in scripts would apply to this folder too. ## src In order to let other packages import your python libraries in ROS you must follow a few steps that have to do with the `src` folder. They are annoying the first time, but they are always the same. You have to create `your_package/src/your_package` folder. Inside of it you must create an empty `__init__.py` file. This is the way Python knows there are Python files for importing. In order to install (and be able to import) this code you must create a `setup.py` file at the root of your package `your_package/setup.py` that contains where this python code is, like: ````python #!/usr/bin/env python ## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD from distutils.core import setup from catkin_pkg.python_setup import generate_distutils_setup setup_args = generate_distutils_setup( packages=['your_package'], package_dir={'': 'src'} ) setup(**setup_args) ```` And you must add to your `CMakeLists.txt` just after `catkin_package()`: catkin_python_setup() **DON'T CALL YOUR LIBRARY NAME EXACTLY LIKE YOUR PACKAGE OR IT WON'T BE ABLE TO BE IMPORTED** For some unknown (to me) reason you cannot have `your_package/src/your_package/your_package.py`. If any other package tries to do from your_package.your_package import SOMETHING It will fail with: ImportError: No module named your_package.your_package