Custom Eclipse PyDev debugging under Python 3.x honest-Unicode and Cyrillic

One cold winter evening, I suddenly remembered that did not share a simple secret of how to achieve a simple Python happiness outside of ASCII of reservations for this wonderful free IDE as Eclipse with the PyDev plug-in. And happiness with debugging of Unicode and honest, which means the following: if you named your variable in Cyrillic, you will be able to see her value by putting a breakpoint, write a few lines of text in Russian and you will not fall off.
Yes, dear reader, Eclipse PyDev is not very friendly with characters beyond 0x7F and debug it likes to fall off every time you try to read the Cyrillic variable. Yes there is, simply hover your mouse on unicodify symbol leads to fatal consequences when debugging code written in Python 3.x (UTF-8). If the settings of your file system different from UTF-8, congratulations, you won't even be able to run your script. I mean exactly what for example under Windows your wonderful script with a single word in Russian just incapacitate PyDev.
Maybe I overdid it, exaggerating, don't worry, to fix this as we are just reading this manual. As a reward we get a free authoring tool, quite comfortable, extremely flexible settings and the improvement, up to development in several languages, with built-in versioning tool.
Under the cut instruction and obscene pictures.

Prepare the spaceport


Our future universal ship called Eclipse downloaded without the development tools for Python is PyDev Eclipse extension, we put it immediately after downloading. Click download page any version of Eclipse. I would recommend "Eclipse IDE for C/C++ Developers", still very nice work Python extensions in C/C++, and indeed it is perfectly compatible with C++ code thanks to the Boost library.Python.
Upon completion of the downloading, extract the eclipse folder anywhere on your computer, consistent with your faheem.
Just run eclipse\eclipse, he probably just started, but if you have never used a JDK, it will request to work, without it will not start. JDK is Java Development Kit, downloaded from the Oracle website, the download page.
When you run Eclipse will ask you where to save all projects, point it at your Workspace (remember that word, you it in the settings menu to look). Finally booted, it is a child of Java will show you your Welcome, don't be scared, just close it.
Somehow here so it will look like at the end of our excursion:

/ > image

In the meantime, let's start with the most important, from-for what cheese-pine forest — encoding. Most importantly make sure that your workplace is set by default to UTF-8.
Go to menu Window => Preferences choose there are General => Workspace in the bottom of the page you need to specify the "Text file encoding" if it is not set to UTF-8 by default, enter the string value in field Other, as shown in the picture:

image

Total, we have to begin to develop the application. Oh no, not all, Python to write and debug, still nothing. Need the PyDev plug-in.

Preparing for the landing on Python 3


Installing PyDev is probably the easiest part of the work on setting up a workplace.
Go to menu "Help = > Eclipse Marketplace..." type in the search string PyDev and click in found plug-button [Install]

image

Then agree with everything, waiting for the download and installation, then we will offer to restart Eclipse, why restart again, please.
After installation, we are invited to play hide and seek with the established environment — the prospect (Perspective) to develop in Python, I will tell you where to find it, it will switch to the PyDev perspective. In the upper right corner there is a row of small buttons to switch perspectives, there is usually something like C++, Debug, Java, SVN and mA-and-scarlet plus sign to the left of them, so it's necessary to click to add the PyDev perspective.
image

In the opened window, Open Perspective, select the development environment Python PyDev viewstony.
We should immediately after choosing to switch to the PyDev perspective menu in the main window should occur some changes that you may not notice, but they switch creating new projects and files and debugging in development mode Python.
Almost all. We are ready to configure and run your first project on Python 3 in Eclipse PyDev.

First draft


Well, let's create our first project.
File menu => New => PyDev Project, fill in name, select the version of Python 3.x and now the most important thing: you need to specify where the Python interpreter, you just need to click on the link "Please configure an interpreter..."

image

Now click Finish.

Our first script


It's time to try running our script as well to know what is in this article.
Menu "File = > New = > PyDev module", or simply right-click on the project "New = > PyDev module", provide our new script name and click Finish.
The script file will be automatically created in the encoding specified in the settings Workspace, which we asked in the beginning. In Python 3.x all code according to the standard UTF-8.
To verify Unicode support write a script like this:

the
word = "First was the word!.."
print(word)

It's time to debug!
Click on the green bug (De-Bug) or just F11. Select Python Run (this is clearly not a unit test). Go...
... Come!..

image

Hehe, but it's free!
Look in the file where we fell and we see a trivial problem with the encoding in the _pydev_execfile.py
Open the link in stacktrace the Eclipse console.
The very first line in the file as it suggests that for Python 3.x here has not rolled not that horse, even a pony lay down.
Here is our remarkable file:

the
#We must redefine it in Py3k if it's not already there
def execfile(file, glob=None, loc=None):
if glob is None:
import sys
glob = sys._getframe().f_back.f_globals
if loc is None:
loc = glob
stream = open(file, 'rb')
try:
encoding = None
#Get encoding!
for _i in range(2):
line = stream.readline() #Should not raise an exception even if there are no more contents
#Must be a comment line
if line.strip().startswith(b'#'):
#Don't import re if there's no chance that there's an encoding in the line
if b coding' in line:
import re
p = re.search(br"coding[:=]\s*([-\w.]+)", line)
if p:
try:
encoding = p.group(1).decode('ascii')
break
except:
encoding = None
finally:
stream.close()

if encoding:
stream = open(file, encoding=encoding)
else:
stream = open(file)
try:
contents = stream.read()
finally:
stream.close()

exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script


A critical eye cast a glance at the battlefield and find some stocks:
1. For Python 3.x encoding will always be None that is fatal in Windows where to set the file system is not UTF-8, and indeed it is wrong to require first line specifies the encoding from a file with code Python 3, which is always UTF-8.
Dressed trivial test sys.version_info[0] >= 3
2. For files starting with a BOM is not cut first character, and the compile is very sensitive to anything extra, he just doesn't understand the characters at the start of script execution.
Without going into details, we can fix everything in a hurry, like so:

the
def execfile(file, glob=None, loc=None):
import sys
if glob is None:
glob = sys._getframe().f_back.f_globals
if loc is None:
loc = glob
if sys.version_info[0] > = 3:
encoding = 'utf-8'
else: 
stream = open(file, 'rb')
try:
encoding = None
#Get encoding!
for _i in range(2):
line = stream.readline() #Should not raise an exception even if there are no more contents
#Must be a comment line
if line.strip().startswith(b'#'):
#Don't import re if there's no chance that there's an encoding in the line
if b coding' in line:
import re
p = re.search(br"coding[:=]\s*([-\w.]+)", line)
if p:
try:
encoding = p.group(1).decode('ascii')
break

encoding = None
finally:
stream.close()

if encoding:
stream = open(file, encoding=encoding)
else:
stream = open(file)
try:
contents = stream.read()
if sys.version_info[0] >= 3 and contents.startswith('\uFEFF'):
contents = contents[1:] #Remove BOM
finally:
stream.close()

exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script


After this small fix we have all performed just fine. I just have to talk about debugging and the small parameter, without which our spaceship will not fly then run the code.

debug Mode and Unicode


Put a breakpoint on the second line, we see the contents of the variable word — this is done by double clicking on the area to the left of the desired line of code.

image

Run debug mode via the F11 or click the green bug in the toolbar. Stand on breakpoint.
(By the way, a digression, when you switch to the Debug perspective, take a few minutes to drag all the junk down.)
Now try to do the following: hover over the variable word the mouse cursor. You're probably wondering what's inside?..
... Oh, fell again!.. This time, quite mysterious and the script ended, but the debugging process died in agony.
But it's free!..
Although there is already quite easy — going in eclipse.ini in the neighborhood of the executable module and add there the line:

the
-Dfile.encoding=UTF-8

Persistent Enescu, restart Eclipse for the changes to take effect.
All.
Now you can debug as you want!
Call variables, at least the Cyrillic alphabet, though in Hindi, name in Russian classes and methods, anything!
I'm not saying it's right, I'm just giving you this opportunity, or rather returning what is taken away gluttonous a free Eclipse PyDev.

Useful links


I decided to give a link to the archive is configured to develop in Python 3.x Eclipse PyDev: eclipse.7z (~171 MB)
After downloading you will still have to do the following:
1) — reconfigure a Workspace folder
2) — set Window => Prefereces => General => Workspace to UTF-8
3) — open the PyDev perspective (initially hidden) via Open Perspective
4) — when creating first project to specify the location of the Python interpreter

Opinion


You'll probably ask me why I did not give immediately, in the first paragraph, to download the archive with the patched Eclipse with PyDev.
I answer, that I decided to share the experience simple, but very useful solutions that's impressive PROFIT in developing in Python 3.x.
You do not should go through the steps and get a great development tool, but also to understand some of the subtleties in the work of Python 3 and the difference with the old and not always kind Python 2.x.
Remember: you don't have to limit yourself to ASCII and in General limit yourself to anything.
Good luck in development!
Article based on information from habrahabr.ru

Популярные сообщения из этого блога

Approval of WSUS updates: import, export, copy

The Hilbert curve vs. Z-order

Configuring a C++ project in Eclipse for example SFML application