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

喜欢而已 提交于 2019-11-29 17:33:18

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.

I spent a full day tearing my hair out while trying to find a solution to this problem.

First I thought there is some problem with $PYTHONPATH. I followed clues given in this (closed) issue on the SublimeREPL GitHub page: https://github.com/wuub/SublimeREPL/issues/355

And while the solution given by the plugin's author didn't work, adding the PYTHONPATH variable containing a path to my working directory to the extend_env key in Packages/SublimeREPL/config/Python/Main.sublime-menu solved the failing imports problem.

I thought I was golden, then I tried to read a text file in Python and got the IOError. Then it finally got to me that the wrong CWD (current working directory) was the culprit.

As the Python docs specify:

"When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the
  • shell variable PATH). the installation-dependent default.

I tried import os and print os.getcwd() in the REPL promt and surely what I got was:

/opt/sublime_text/

So what I did? The solution was a simple change to a line in Packages/SublimeREPL/config/Python/Main.sublime-menu (in a section corresponding to the REPL I was using). From:

"cwd": "$file_path",

to:

"cwd": "$folder",

Now calling os.getcwd() resulted in a path to the directory containing the file I sent to SublimeREPL and everything worked. Why $file_path gives '/opt/sublime_text' rather than the path to a file is either a question to the plugin's author or some underlying quirk with my system.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!