简体   繁体   English

为什么SublimeREPL无法导入Python模块或不读取CWD中的文件?

[英]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. 我正在使用SublimeREPL插件在Ubuntu Zesty上使用Sublime Text 3 build 3126。

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. 我使用CTRL + SHIFT + P >> SublimeREPL打开Python REPL :Python,并使用CTRL +,F和everything将文件发送到REPL,只要我使用单个Python脚本文件即可。 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. 一旦我尝试将代码的某些部分移至另一个模块并将其导入主文件中,就会收到讨厌的ImportError:没有名为 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:Python的文件/面板与print os.getcwd()的输出之间是否存在关系? SublimeREPL initializes its cwd at its startup of the REPL. SublimeREPL在REPL启动时初始化其cwd。 The cwd will stay for the entire lifetime of the REPL. cwd将在REPL的整个生命周期内保持不变。

As you pointed out, the default cwd for SublimeREPL: Python is: 正如您所指出的, SublimeREPL:Python的默认cwd是:

"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. 这意味着以空的Sublime面板中的光标启动SublimeREPL:Python会导致/opt/sublime_text/作为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. 但是,如果在光标位于要运行的.py文件中时启动SublimeREPL:Python ,则REPL将使用当前.py文件的文件夹作为其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的设置更改为:

"cwd": "$folder",

the above behaviour of the stays unchanged for cases where you're not using sublime-projects. 对于不使用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. 当您确实使用sublime-project时,python REPL cwd将成为主项目文件夹,而与REPL启动时的光标位置无关。 This might be the desired behaviour if you have a python-driven project. 如果您有python驱动的项目,则这可能是所需的行为。 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. 但是,如果您只是在较大的项目中使用某些python脚本,则可能不希望主项目文件夹成为python REPL中的cwd。

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. 首先,我认为$ PYTHONPATH存在一些问题。 I followed clues given in this (closed) issue on the SublimeREPL GitHub page: https://github.com/wuub/SublimeREPL/issues/355 我关注了SublimeREPL GitHub页面上此封闭期刊中给出的线索: 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. 并且尽管插件作者提供的解决方案不起作用,但将Packages / SublimeREPL / config / Python / Main.sublime-menu菜单中包含我工作目录路径的PYTHONPATH变量添加到了extend_env键中,可以解决导入失败的问题。

I thought I was golden, then I tried to read a text file in Python and got the IOError. 我以为自己很黄金,然后我尝试用Python读取文本文件并得到IOError。 Then it finally got to me that the wrong CWD (current working directory) was the culprit. 然后终于让我知道错误的CWD(当前工作目录)是罪魁祸首。

As the Python docs specify: 如Python文档所指定:

"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: “导入名为spam的模块时,解释器首先搜索具有该名称的内置模块。如果找不到,则解释器将在变量sys.path给出的目录列表中搜索名为spam.py的文件。 sys.path从以下位置初始化:

  • the directory containing the input script (or the current directory) . 包含输入脚本的目录(或当前目录)
  • PYTHONPATH (a list of directory names, with the same syntax as the PYTHONPATH(目录名称列表,语法与
  • shell variable PATH). shell变量PATH)。 the installation-dependent default. 取决于安装的默认值。

I tried import os and print os.getcwd() in the REPL promt and surely what I got was: 我尝试在REPL promt中导入os打印os.getcwd() ,我肯定得到的是:

/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). 解决方案是对Packages / SublimeREPL / config / Python / Main.sublime-menu (在与我所使用的REPL相对应的部分)中的一行进行简单更改。 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. 现在调用os.getcwd()导致到目录的路径,该目录包含我发送给SublimeREPL的文件,并且一切正常。 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. 为什么$ file_path给出'/ opt / sublime_text'而不是文件的路径,这是插件作者的问题,还是我系统的一些潜在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM