我对 python 和 sublime 还很陌生。我目前正在观看培训视频并跟进。我遇到了很多关于 sublime 的问题,但无法通过这个问题。尝试将模块导入我在训练期间创建的解释器时出现错误。 wiper.py
文件位于 Users/marcus/Documents/Scripts/
。我已经在下面打印了当前路径。有人可以帮助我吗?
>>> import wiper
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named wiper
>>> print ('PATH = ' + os.environ['PATH'])
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/Users/marcus/Documents/Scripts//.rvm/bin:de here
一些附加信息。我导入了 sys 并运行了 sys.path 并获得了这些信息。我不知道这是否有帮助。
>>> import sys
>>> sys.path
['',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
您需要将 /Users/marcus/Documents/Scripts
添加到您的 Python 模块搜索路径。 PATH
仅供您的 shell 查找可执行文件,而不是 Python 模块。
您可以设置 PYTHONPATH
environment variable :
export PYTHONPATH='/Users/marcus/Documents/Scripts'
您可以只为您的 Sublime Text 项目设置该环境变量;在您的项目设置中添加:
"build_systems":
[
{
"name": "Python",
"shell_cmd": "python -u \"$file\"",
"env": {
"PYTHONPATH": "/Users/marcus/Documents/Scripts"
},
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
]
或使用相同类型的设置来创建全局 build system .
另一种方法是让您的脚本通过插入 sys.path
以编程方式添加路径:
import sys
sys.path.insert(0, '/Users/marcus/Documents/Scripts')
import wiper
我是一名优秀的程序员,十分优秀!