1

I have two files, both on my Desktop:

sotest.py

print 'Hello world'

sotest2.py

foo = open ('sotest.py', 'r')

I'm running Python out of Notepad++ as follows (from Use IDLE to launch script using the full file path):

C:\Program Files (x86)\Python27\Lib\idlelib\idle.bat -r "$(FULL_CURRENT_PATH)"

Running sotest2.py returns the following:

Traceback (most recent call last):
  File "C:\Users\doncherry\Desktop\sotest2.py", line 7, in <module>
    foo = open ('sotest.py', 'r')
IOError: [Errno 2] No such file or directory: 'sotest.py'

When running the same file out of Pyhton's IDLE, it runs without errors. If I change sotest2.py to the following, it works out of Notepad++ without problems.

import sotest

Which part of my configuration do I need to change to make the opening work? I'd prefer to change some setting in Notepad++ rather than adding code to each of my Python files.

Community
  • 1
  • 1
doncherry
  • 259
  • 3
  • 14
  • Try to print out your working directory (os.getcwd()). Most likely it is set to a path where notepad++ starts which is not your desktop – Yevgen Yampolskiy Nov 10 '12 at 03:09
  • @YevgenYampolskiy: Correct, it returns `C:\Program Files (x86)\Notepad++`. Can I pass the working directory to IDLE from Notepad++? – doncherry Nov 10 '12 at 03:15
  • If you remove the -r from the command, it starts in the correct directory, but then it doesn't run the script automatically... – ChimeraObscura Nov 10 '12 at 04:09
  • @ChimeraObscura: So ... then it just opens an empty IDLE? How would I get the script running from there? – doncherry Nov 10 '12 at 04:23
  • I'm using the following command in notepad++, and it opens the current file in idle's editor: `E:\Python27\Lib\idlelib\idle.bat "$(FULL_CURRENT_PATH)"` – ChimeraObscura Nov 10 '12 at 04:33

1 Answers1

0

import sotest works because it searches sotest module in sys.path and the sotest2.py's directory is added to sys.path if you run sotest2.py as a script. If sotest2.py and sotest.py were in different directories then unless something else put sotest.py's directory into pythonpath then import sotest inside sotest2.py would fail.

To open sotest.py inside sotest2.py as a file you could specify its absolute path. Assuming they are in the same directory you could find it automatically:

# in sotest2.py:
import os

scriptdir = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(scriptdir, 'sotest.py')
with open(path) as sotest_file:
     text = sotest_file.read()
jfs
  • 399,953
  • 195
  • 994
  • 1,670