Robert Basic's blog

Posts tagged 'python'

Force Python version in Vim

by Robert Basic on January 12, 2017.

Vim can be compiled with Python support. Vim can be compiled with both Python 2 and Python 3 support.

At the same time.

But not really.

Vim can have both of them, but use only one at a time. If you start using one version, there is no way to switch to the other one.

The silly thing is that if you simply ask Vim which version does it support, the first one asked and supported is going to be the one loaded and used. Trying to use the other one from that point will result in an error.

if has('python')
elif has('python3')
endif

Guess which one is loaded? Python 2.

Try calling Python 3 and ka-boom!

:py3 print('hello')
E836: This Vim cannot execute :py3 after using :python

Switch the order around:

if has('python3')
elif has('python')
endif

And now? Yup, Python 3.

Why is this ridiculous? Because if you have a bunch of Vim plugins loaded, the first one that asks for a specific Python version wins! Reorder the plugins and suddenly a different Python version is loaded.

Gotta love the software development world.

Luckily, this can also be used to fix the problem itself.

How?

Force one of the Python versions from the top of your .vimrc file:

if has('python3')
endif

Now you can have a little bit of sanity and be sure what Python version is Vim going to use. Of course, doing this might break plugins written solely for Python 2, so do it at your own risk.

Happy hackin’!

Tags: python, vim, vimrc.
Categories: Development, Software.

Installing Python2 with Ansible

by Robert Basic on June 29, 2016.

Ansible uses Python2 to run the provisioning commands on the host machines. At this time it does not support Python3, which is the default python version in Fedora releases for quite some time now.

So to be able to manage Fedora machines with Ansible, I need to install Python2, but how to install it when all the Ansible modules depend on Python2 being installed? Turns out it’s quite simple, by turning of the gathering of facts in Ansible and using the raw module to install the required packages:

- hosts: all
  gather_facts: no
  become: yes
  tasks:
    - name: Install python2 and python2-dnf
      raw: dnf -y install python2 python2-dnf
    - name: Gather facts
      setup:

Just remember this needs to be the very first thing that happens on all your Fedora hosts. After python2 is installed, gather the facts for all the hosts by running the setup module.

Happy hackin’!

Tags: ansible, fedora, provisioning, python, vagrant.
Categories: Development, Software.

Import custom Python modules in Vim plugins

by Robert Basic on June 16, 2016.

This took me a while to figure out so I’m writing it down for future self and anyone else who needs it.

I started writing a new Vim plugin that will use the Python interface as most of the work will be done there, mostly to keep my sanity.

Having a plugin layout such as:

.
├── lib
│   └── mypymodule
│       └── ham.py
└── plugin
    └── my-vim-plugin.vim

I want to be able to do a

from mypymodule import ham

from within the my-vim-plugin.vim file.

For that to happen the <sfile> command line special comes to rescue. It is the file name of the sourced file in Vim, that is, the file name of the Vim plugin. Using the :p and :h file name modifiers it gives us the full path to the plugin directory of our plugin.

" This will give something like
" /home/robert/projects/my-vim-plugin/plugin
let g:plugin_path = expand('<sfile>:p:h')

And here comes the kicker: the <sfile> needs to be expanded outside of our Vim function where it is used, otherwise the <sfile> points to the path of the file that called the Vim function.

In code, getting the path to the sourced file from within the Vim function would be incorrect:

" ./plugin/my-vim-plugin.vim
function! MyVimPlugin()
python << endpython

import vim

vim.command("let a:plugin_path = expand('<sfile>:p:h')
plugin_path = vim.eval("a:plugin_path")
print plugin_path

endpython
endfunction

because it would end up printing the current working directory from where the MyVimPlugin function is called.

The correct way to do is to get the path to the sourced file outside of the Vim function:

" ./plugin/my-vim-plugin.vim
let g:plugin_path = expand('<sfile>:p:h')

function! MyVimPlugin()
python << endpython

import vim

vim.command("let a:plugin_path = expand('<sfile>:p:h')
plugin_path = vim.eval("a:plugin_path")
print plugin_path

endpython
endfunction

This way the path is set at the time the plugin is sourced and not at the time when function is called.

Finally, to be able to import the mypymodule from the lib, we need to point to the lib directory and add it to the system paths. Complete example:

" ./plugin/my-vim-plugin.vim
let g:plugin_path = expand('<sfile>:p:h')

function! MyVimPlugin()
python << endpython

import os
import sys
import vim

" Get the Vim variable to Python
plugin_path = vim.eval("g:plugin_path")
" Get the absolute path to the lib directory
python_module_path = os.path.abspath('%s/../lib' % (plugin_path))
" Append it to the system paths
sys.path.append(python_module_path)

" And import!
from mypymodule import ham

endpython
endfunction

By the way, here’s the documentation for the Vim Python module.

Happy hackin’!

Tags: plugin, python, vim.
Categories: Development, Programming.

pugdebug 1.0.0.

by Robert Basic on July 01, 2015.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

After 3 months since announcing that I’m working on pugdebug, and some 5 months since I actually started working on it, it is finally time to let version 1.0.0 out in the wild.

It’s been a busy 3 months: 82 pull requests got merged, 67 issues resolved, more than 350 commits pushed. A lot of changes, fixes and improvements found their way into this first version.

First of all, a big thanks goes out to Ivan Habunek and Srdjan Vranac for helping. They asked for and added new features, tested on Windows and OSX systems, helped fleshing out ideas.

One of the biggest news is that there are binaries built for Linux and Windows operating systems, using pyinstaller. These binaries include everything pugdebug needs to work so there is no need to install anything. Just download the binary for your system and run it. That’s it. It makes me incredibly happy that it’s possible to have it this simple to run and use pugdebug.

pugdebug

The user interface has improved a great deal. It is using dockable widgets for different pieces of the UI, making the layout of the application configurable by just dragging the widgets around. It’s not all great though, there’s still room for improvement, but it will be better over time.

pugdebug allows to debug multiple requests one after the other which helps debugging in a more “complicated” scenario where there are, for example, multiple AJAX calls triggered in succession. By starting to listen to incomming connections, pugdebug will listen to all incoming connections and, based on the IDE key setting, decide should the connection be queued for debugging, or ignored.

It is also now possible to create projects inside pugdebug, as a way to help switching between different PHP projects where debugging is needed. Simply name the project, set it’s settings and that’s it. pugdebug stores all the configuration files in it’s own directory, so nothing will be added to your PHP projects.

I’m especially happy and proud that pugdebug got included on Xdebug’s website on the list of remote debugging clients. Thanks to Derick Rethans!

For more information on how to use pugdebug, take a look at the read me file or the wiki and let me know if you have any issues with it!

Tags: debugging, pugdebug, pyqt, python, qt, stable, xdebug.
Categories: Development, Programming, Software.

Introducing pugdebug

by Robert Basic on April 01, 2015.
Heads-up! You're reading an old post and the information in it is quite probably outdated.

In my spare time in the past few months I was working on a tool that would help me in my every day job as a PHP programmer. As you may, or may not, know, I’m using vim as my editor/almost IDE, but one thing that is missing from it is the ability to debug PHP files remotely. Yes, there are a bunch of plugins out there that add debugging to vim, but none of them felt usable for me.

And based on my google searches, there are no standalone remote debuggers for PHP, that work on Linux.

In February this year I started to work on a desktop application that would help me address this issue.

pugdebug

pugdebug  is a PyQt desktop application meant to be used as a remote debugger for PHP, that communicates with xdebug.

It is meant to be a debugger and only a debugger. There are a plenty of (good) IDEs that include remote debugging and I’m not going to start writing another one (although I did start one, once).

The application is still pretty simple, ugly as hell, but it works. Sort of. There are still a few kinks left to sort out and I’m doing my best to write them all down.

It’s dependencies are Python 3.4, Qt 5.4, SIP 4.6 and PyQt 5.4. The read me file should have a bit more details on how to start using it. I know it’s a bit messy to set everything up, but I am working on building executables for different Linux distros. That stuff is hard!

It is lincesed under the GNU GPL v3 license, because PyQt.

Using pugdebug

Start the application, click the start button and then it waits for incomming connections. Load a PHP page to start a HTTP debug session, and pugdebug should break on the first line of your code.

Stepping around the code is possible with the step into, step out and step over commands. At each step, pugdebug will get the current variables state from xdebug and display them.

Double clicking on lines in the code viewer will set breakpoints on those lines. Do note though that there needs to be a debugging session active to be able to set a breakpoint. This will change in the (near) future.

And that’s about it. While I know it doesn’t look like much, this was, and still is, a nice learning experience for me and the best part of it is that I was using pugdebug earlier this week to debug a PHP application I’m working on.

Tags: debugging, pugdebug, pyqt, python, qt, xdebug.
Categories: Development, Programming, Software.