Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

sublimetext3 - Why SublimeREPL won't import a Python module or read a file in CWD?

I'm using Sublime Text 3 build 3126 on Ubuntu Zesty with the SublimeREPL plugin.

I open a Python REPL with CTRL+SHIFT+P >> SublimeREPL: Python and send a file to REPL with CTRL+, , F and evertyhing works as long as I work on a single Python script file. As soon as I try to move some parts of my code to another module and import it in the main file, I get the nasty ImportError: No module named error.

Trying to open a text file located in the same directory with:

codecs.open('filename', encoding='utf-8')

results in:

 IOError: [Errno 2] No such file or directory: 'filename'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did you investigate whether there was a relation between the file/panel from where you opened SublimeREPL: Python and the output of print os.getcwd()? SublimeREPL initializes its cwd at its startup of the REPL. The cwd will stay for the entire lifetime of the REPL.

As you pointed out, the default cwd for SublimeREPL: Python is:

"cwd": "$file_path"

This means that starting SublimeREPL: Python with your cursor in a blank Sublime panel, results in /opt/sublime_text/ as the cwd. This could be considered undesired and confusing.

However, if you start SublimeREPL: Python while your cursor is in the .py file that you want to run, the REPL will use the folder of your current .py file as its cwd.

Personally I think this is the desired behaviour, but you have to know it before it becomes convenient.

If you change the settings for the cwd to:

"cwd": "$folder",

the above behaviour of the stays unchanged for cases where you're not using sublime-projects. When you do use a sublime-project, the python REPL cwd will be the main project folder, independent of the cursor location at REPL startup. This might be the desired behaviour if you have a python-driven project. However, if you are just using some python scripts in a bigger project, you probably don't want your main project folder to be the cwd in the python REPL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...