开发者

Python script runs fine from command line, file not found error when using Eclipse and PyDev

Folder Structure:

src
+ root
+ nested
  ++ myprogram.py
  ++ helper.py
  ++ res
    +++ excluded.txt
    +++ whitelist.txt

Case 1 - Create a PyDev project in eclipse. Run as a PyDev configuration. Specify main module and everythin. Main module is myprogram.py. When run using eclipse, I get the error - IOError: [Errno 2] No such file or directory: '.\res\excluded.txt' excluded.txt is specified as => excluded_words_file = r'.\res\ex开发者_开发问答cluded.txt', inside myprogram.py

Case 2. Run the script myprogram.py using command line. Runs perfectly fine.

The only difference is that Eclipse is also using SVN source control for this project. So I tried after disconnecting the project from the repository. But still the same issue.

So what might I be doing wrong in the Eclipse setup? Thanks.


A quick debug with :

import os
print os.getcwd()

should let you see eclipse is not running from the same dir you place yourself when invoking from commandline.

From the python module -> right click -> properties -> run/debug settings -> python run -> arguments, you can change the working directory. IMHO is not a good idea, better change the program so it reads from an absolute path, in order to have it work no matter where is deployed.


Sounds to me a like a path issue. Post what you see when you do

import sys
print sys.path

OP says that sys.path outputs the following

C:\Documents and Settings\sumod_pawgi\workspace\SWCTester\src\root\nested 
C:\eclipse\plugins\org.python.pydev.debug_2.1.0.2011052613\pysrc 
C:\Documents and Settings\sumod_pawgi\workspace\SWCTester\src 
C:\Python25\Lib\site-packages 
C:\Python25 
C:\Python25\DLLs 
C:\Python25\lib 
C:\Python25\lib\lib-tk 
C:\Python25\lib\plat-win 
C:\WINDOWS\system32\python25.zip 
C:\eclipse\plugins\org.python.pydev_2.0.0.2011040403\PySrc 
C:\Python25\Lib\site-packages\py2exe

From what I see, I can identify as a problem: your res directory is not in your path. This can be fixed in one of two ways:

  1. do sys.path.append('path/to/res/directory')
  2. add the res directory to the path in PyDev Run Configurations

OR

  1. Right Click on *.py file (in Eclipse), select 'Run as'-> 'Run Configurations...'
  2. On the right hand side, Select 'Arguments' tab.
  3. Verify 'Working Directory' section, check selected working directory (all required files should be present in there - if not click on 'workspace' button and select folder from workspace)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜