14 Dec

Some problems about configuring pydev console in eclipse.

I’m planning to do some java coding now. So I installed eclipse which allows me to work with both python and java. However I was really annoyed at configuring the pydev console.
Firstly, I found I can not interact with the console if I run the script with “run” button. I must activate an interactive console to do some further interaction. But the pydev’s console behaves in an odd manner, at least, that’s how it seems to me.
The interactive console can not import modules right there in the same directory with the current working script. The solution is to add the project’s directory to pydev’s PYTHONPATH: right click project name –>PyDev–>set as source folder (add to PYTHONPATH).
Then comes the next problem. I have two similar projects. Both of them rely on a ui.py script of its own. To run these projects, I have to add their directory to Python path as described above. Then I ran project1, everything is fine. Then I ran project2, some error occured. It turns out that project2 called ui.py(1) insteat of its own ui.py. Obviously, the pythonpath configuration does not work in my situation. I have to try another solution, that is to add the projects’s directry to their external lib.
But I still can’t get my project to run correctly. This time, the console failed to find a xls file which was supposed to be read. I typed import os; print(os.getcwd()) to see the current working directory. It turns out that the interactive console was working under eclipse’s default working path rather than the project’s path. So, I have to edit the initial command of interactive console to force the console to change it’s working directory at start up. Go to Windows–>preferences–>Pydev–>interactive console–>initial command, change the initial interpreter commands to:

import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os
import inspect

__old_runfile = runfile

def runfile(file):
curpath = os.path.dirname(os.path.abspath(file))
os.chdir(curpath)
__old_runfile(file)

Now I can properly run my project. I have solved all the problems though, I think I will not use eclipse a lot on python coding. I’d still like to use spyder which is far more cute than eclipse especially in virtual environment. It really saved you a lot of time from configuring all these paths and resourses

12 Jun

Problems about compiling (with pyinstaller).

It’s really a huge work for me this week to compile my nlk program with new interface into exe. I ran into a lot of problems. Fortunately, I managed to solve them through Googling. I will note some of the major problems below.

1.No module named *** :
At firse I was trying py2exe. However, a lot of modules can not be included in py2exe. So I turned to pyinstaller. I can get a exe file with pyinstaller, but when i ran it, a command window flashed out and report “No mudule named ***”. I included the missing files in the bat file one by one according to the reports. That leads my bat file to be very lengthy:

pyinstaller -F -w myscript.py --hidden-import=scipy.special._ufuncs_cxx --hidden-import=scipy.integrate --hidden-import=scipy.integrate.quadrature --hidden-import=scipy.integrate.odepack --hidden-import=scipy.integrate._odepack --hidden-import=scipy.integrate.quadpack --hidden-import=scipy.integrate._quadpack --hidden-import=scipy.integrate.quadpack --hidden-import=scipy.integrate._ode --hidden-import=scipy.integrate.vode --hidden-import=scipy.integrate._dop --hidden-import=scipy.integrate.lsoda
 pause

Most of the files are from scipy.integrate. I guess there should be a simpler method to include the whole folder with one command which i don’t know at the moment.

after all these test-and-modify, I finally got an “executable” executable file.

2. get rid of the black window on running .exe: this is very simple. Just as the bat file shown above, a simple “-w” solved this problem.

3. file already exists but should not: ***pyconfig.h:
Before my program opened its window, there was another window which reports: file already exists but should not: *****(a path which i can not remember now)/pyconfig.h.
I found the solution here:
when I start the compiling Bat file, a .spec file which contains the compiling configuration information was generated in the same folder with the file name “myscript(.spec)”. What i have to do is to modify this spec file. just like the answer in the above link, I have to add after a=Analysis something like this :

for d in a.datas:
    if 'pyconfig' in d[0]: 
        a.datas.remove(d)
        break

Then, I have to modify the bat file. Because All the required compiling configurations are in the spec file now, I should not re-run the previous bat file to over write spec, but call the spec file directly in bat. The new bat file is very short:

pyinstaller nlk.spec
 pause

4.myscript.exe not a valid win32 application:

My exe worked fine on win64. But when I ran it on a 32bit XP, it reported “myscript.exe not a valid win32 application”.

My script was written and compiled in a 64-bit version Winpython. It seems that I have to install a new 32-bit winpython.

So I installed 32-bit winpython and installed qtutil and pyinstaller which was not contained in winpython.I compiled the script in 32wp, and ran it again. Still, it reported the same error. Then I found that although I had unregistered the 64wp and registered the 32wp, the path in environment variable still point to the 64wp.So I modified the environment variable to 32wp and tried again. This time the compiling comand window reported a new error:

5.pyinstaller distributionnotfound:

Solution to this error is:

First, up date pip to latest version:

python -m pip install --upgrade --force pip

Second, re-install pyinstaller with the new pip (I’m not sure if this step is necessary).

Third, compile with the new pyinstaller.

 

After all these strivings, I finally got an executable file that worked perfectly on both 64bit and 32bit windows systems. Special thanks to GOOGLE without which I might never complete my work.

27 May

“Qtutils” is a useful pack

I come up with an problem: http://stackoverflow.com/questions/30180046/statusbar-doesnt-show-message-immediately-and-sometimes-crash

I created an interface in pyqt4 designer, there is only a button and a status bar. I want to show a message in the status bar when I click the button. For some reason I have to put this action in a new thread.

But when I click the button, nothing happened. The message didn’t appear until I drag the edge of the window. Sometimes the click action even cause the program to crash

as three_pineapples answered :”The crash happens because you are interacting with the Qt GUI from a secondary thread. You are only allowed to interact with the GUI from the main thread”.

He provided a very useful pack “qtutils”.

I define my interaction action as a new fuction, say shili(self). When I want to interact with GUI from a secondary thread, I simply write :

inmain(self.shili)

10 May

Chinese character coding problem in pyqt4

I want to save a file with a Chinese name in pyqt.

It reports: IOError: [Errno 22] invalid mode ('w') or filename: PyQt4.QtCore.QString(u'D:/Users/zhout_000/Desktop/\u672a\u547d\u540d.dxf')

Reason: the type of the filename read from the file dialog is infact “QString”, not”string”

Solve: convert QString to unicode: fileName=unicode(QtCore.QString(fileName))

24 Mar

“spyder error: ‘import sitecustomize’ failed; use -v for traceback

#this article was transferred from my Blogger.

win8 spyder

the programm is totally correct, and it did give a correct out put

but after the out put there is an error message reads:”spyder error: ‘import sitecustomize’ failed; use -v for traceback”

solve:#8 NathanH…@gmail.com

I decided to set an environment variable in Windows 7 to see if it would fix the problem.  I made a variable named SPYDER_ENCODING and set it to UTF-8.  I no longer get the error.  Is there some problem that spyder has with windows 7's default encoding?  Does anybody have any ideas?

from: https://code.google.com/p/spyderlib/issues/detail?id=771
24 Mar

py2exe的一些问题。

几个月前编译完全正常的一个py文件昨天重新编译就出了一连串问题。分别解决如下。
1、首先是RuntimeError: maximum recursion depth exceeded导致编译中断。在STO提问得到解决:在setup文件开头添上:
import sys
sys.setrecursionlimit(5000)
即可。

2.虽然编译出了exe,但是得到的exe运行时报告缺少“_validation”。解决办法是在setup文件中将validation手动include进去,像这样:
# mysetup.py
import sys
sys.setrecursionlimit(50000)
from distutils.core import setup
import py2exe
options = {
“py2exe”: {“compressed”:1,
“optimize”:2,
“includes”:[“scipy.sparse.csgraph._validation”,”scipy.special._ufuncs_cxx”,”sympy.printing.gtk”],
“bundle_files”:1}
};
setup(
windows = [“nlk.py”],
options = options,
)
其中的scipy.sparse.csgraph._validation”,”scipy.special._ufuncs_cxx”,”sympy.printing.gtk都是根据后来的报错逐个添加进去的。期间还安装了gtk这个包。至于这是个啥?不知道。

3.最后一个问题是如下报错:
OMP: Warning #178: Function GetModuleHandleEx failed
OMP: System error #126: The specified module could not be found.
STO上一个相关问题给出的建议是去sourceforge重新安装numpy1.8.1。试了但无效。后来发现将setup文件中的bundle_files值改为3就正常。但是这样得到的exe就不是单一文件了,有点不爽。而且体积奇大,6k的脚本编译出来一个近200M的文件夹。

总之呢,这个可执行文件算是有了。优化的事以后再说吧。(:3っ)∋