但可以从命令行导入吗

但可以从命令行导入吗

本文介绍了Sublime Text 3无法导入python模块,但可以从命令行导入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在ST3中使用python进行构建时,在尝试执行操作时出现导入错误

when I tried to build with python in ST3, I get an import error as I tried to do

import caffe

但是当我只是在终端上运行时,键入

but when I simply ran on the terminal, typing

$ python
>>> import caffe

它有效。在我的Sublime文本3上,我仍然可以导入其他模块,例如numpy和matplotlib。

it works. On my sublime text 3 I still can import other modules like numpy and matplotlib.

这是我发现的Sublime python版本(这是正确的位置吗?为什么不正确提取出来,而不是放入包中?):
目录是:/opt/sublime_text/Packages/Python.sublime-package

This is the sublime python build I found (is this the right location? Why is it not extracted out but instead in a package?):The directory is: /opt/sublime_text/Packages/Python.sublime-package

和文件python。在sublime-package中,sublime-build是:

and the file python.sublime-build in the Python.sublime-package is:

{
    "shell_cmd": "python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python -m py_compile \"${file}\"",
        }
    ]
}

在我检查了python路径后:

After I checked my python path:

$ python -c "import sys; print '\n'.join(sys.path)"

我的输出是:

/home/user/caffe/python
/home/user
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/python2.7/dist-packages/wx-3.0-gtk2

和我的dist-packages

and my dist-packages doesn't have caffe as I installed it in home/user instead.

所以我决定在终端机中运行:

So I decided to run in the terminal:

export PYTHONPATH=/home/user/caffe/python:$PYTHONPATH

但是再次检查我的python路径,似乎没有加入。这是原因吗?但是,为什么我可以直接从终端导入咖啡,却不能在ST3中导入咖啡? PS:我确实在用户和其他bashrc个人资料中添加了caffe。

but checking my python path again, it doesn't seem to get added in. Is this the reason? However, why is it that I can import caffe directly from my terminal but not in ST3? PS: I did add caffe to my user and etc bashrc profile.

感谢您的帮助。

推荐答案

您可以在导入caffe之前添加它

You can add this before import caffe

import sys
sys.path.insert(0, '/path_to_caffe_root/python')

这篇关于Sublime Text 3无法导入python模块,但可以从命令行导入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:53