尝试通过以下方式打开位于同一目录中的文本文件:codecs.open('filename', encoding='utf-8')导致: IOError: [Errno 2] No such file or directory: 'filename'解决方案您是否调查了打开 SublimeREPL:Python 的文件/面板与? SublimeREPL在REPL启动时初始化其cwd. cwd将在REPL的整个生命周期中保持不变.正如您所指出的, SublimeREPL:Python 的默认cwd是:"cwd": "$file_path"这意味着在空白的Sublime面板中用光标启动 SublimeREPL:Python 会导致/opt/sublime_text/作为cwd.这可能被认为是不希望的并且令人困惑.但是,如果在光标位于要运行的.py文件中时启动 SublimeREPL:Python ,则REPL将使用当前.py文件的文件夹作为其CWD. 我个人认为这是理想的行为,但是在变得方便之前,您必须了解它.如果将cwd的设置更改为:"cwd": "$folder",在不使用sublime-projects的情况下,上述行为保持不变.当您确实使用sublime-project时,python REPL cwd将成为主项目文件夹,而与REPL启动时的光标位置无关.如果您有python驱动的项目,则这可能是所需的行为.但是,如果您只是在较大的项目中使用某些python脚本,则可能不希望主项目文件夹成为python REPL中的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' 解决方案 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. 这篇关于为什么SublimeREPL无法导入Python模块或不读取CWD中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 16:38