无法导入本地模块
Can't import local module
我在 /opt
目录中安装了一个应用程序并将其根添加到 PATH(对于所有想要使用它的用户)。现在,当我从我的用户调用主脚本时,它工作正常,但其他用户报告相同的错误:
user@server:~$ ragout.py -h
Traceback (most recent call last):
File "/opt/ragout-2.0-linux-x86_64/ragout.py", line 33, in <module>
from ragout.main import main
File "/opt/ragout-2.0-linux-x86_64/ragout.py", line 33, in <module>
from ragout.main import main
ImportError: No module named main
这是主脚本:
#!/usr/bin/env python2.7
#(c) 2013-2014 by Authors
#This file is a part of Ragout program.
#Released under the BSD license (see LICENSE file)
"""
This script does all the necessary preparations
and invokes Ragout
"""
import os
import sys
LIB_DIR = "lib"
#Check Python version
if sys.version_info[:2] != (2, 7):
print("Error: Ragout requires Python version 2.7 ({0}.{1} detected)."
.format(sys.version_info[0], sys.version_info[1]))
sys.exit(-1)
#Setting executable paths
ragout_root = os.path.dirname(os.path.realpath(__file__))
lib_absolute = os.path.join(ragout_root, LIB_DIR)
sys.path.insert(0, lib_absolute)
sys.path.insert(0, ragout_root)
os.environ["PATH"] = lib_absolute + os.pathsep + os.environ["PATH"]
#Ragout entry point
from ragout.main import main
sys.exit(main())
我认为脚本在扩展 ragout_root
和 lib_absolute
时可能会遇到一些问题,所以我在 from ragout.main import main
之前添加了 print(ragout_root, lib_absolute)
以查看发生了什么。现在,当我 运行 我的用户的应用程序时,我得到这个:
me@server:~$ ragout.py -h
('/opt/ragout-2.0-linux-x86_64', '/opt/ragout-2.0-linux-x86_64/lib')
usage: ragout.py [-h] [-o output_dir] [-s {sibelia,maf,hal}] [--refine]
[--solid-scaffolds] [--overwrite] [--repeats] [--debug]
[-t THREADS] [--version]
recipe_file
...
用户得到这个
user@server:~$ ragout.py -h
('/opt/ragout-2.0-linux-x86_64', '/opt/ragout-2.0-linux-x86_64/lib')
('/opt/ragout-2.0-linux-x86_64', '/opt/ragout-2.0-linux-x86_64/lib')
Traceback (most recent call last):
File "/opt/ragout-2.0-linux-x86_64/ragout.py", line 33, in <module>
from ragout.main import main
File "/opt/ragout-2.0-linux-x86_64/ragout.py", line 33, in <module>
from ragout.main import main
ImportError: No module named main
无论出于何种原因,它都打印了两次——尽管路径是正确的——它仍然无法从本地模块导入。有任何想法吗?
我认为这是一个权限问题。请参阅 this Stack Overflow question 用户报告了一个非常相似的问题。
- 您创建了位置,因此您拥有它的权限并且
可以导入。
- 其他用户没有权限,无法从他们无法读取的文件夹中导入。
解决方案:确保所有合适的用户都在对文件夹具有至少读取权限的用户组中。
我在 /opt
目录中安装了一个应用程序并将其根添加到 PATH(对于所有想要使用它的用户)。现在,当我从我的用户调用主脚本时,它工作正常,但其他用户报告相同的错误:
user@server:~$ ragout.py -h
Traceback (most recent call last):
File "/opt/ragout-2.0-linux-x86_64/ragout.py", line 33, in <module>
from ragout.main import main
File "/opt/ragout-2.0-linux-x86_64/ragout.py", line 33, in <module>
from ragout.main import main
ImportError: No module named main
这是主脚本:
#!/usr/bin/env python2.7
#(c) 2013-2014 by Authors
#This file is a part of Ragout program.
#Released under the BSD license (see LICENSE file)
"""
This script does all the necessary preparations
and invokes Ragout
"""
import os
import sys
LIB_DIR = "lib"
#Check Python version
if sys.version_info[:2] != (2, 7):
print("Error: Ragout requires Python version 2.7 ({0}.{1} detected)."
.format(sys.version_info[0], sys.version_info[1]))
sys.exit(-1)
#Setting executable paths
ragout_root = os.path.dirname(os.path.realpath(__file__))
lib_absolute = os.path.join(ragout_root, LIB_DIR)
sys.path.insert(0, lib_absolute)
sys.path.insert(0, ragout_root)
os.environ["PATH"] = lib_absolute + os.pathsep + os.environ["PATH"]
#Ragout entry point
from ragout.main import main
sys.exit(main())
我认为脚本在扩展 ragout_root
和 lib_absolute
时可能会遇到一些问题,所以我在 from ragout.main import main
之前添加了 print(ragout_root, lib_absolute)
以查看发生了什么。现在,当我 运行 我的用户的应用程序时,我得到这个:
me@server:~$ ragout.py -h
('/opt/ragout-2.0-linux-x86_64', '/opt/ragout-2.0-linux-x86_64/lib')
usage: ragout.py [-h] [-o output_dir] [-s {sibelia,maf,hal}] [--refine]
[--solid-scaffolds] [--overwrite] [--repeats] [--debug]
[-t THREADS] [--version]
recipe_file
...
用户得到这个
user@server:~$ ragout.py -h
('/opt/ragout-2.0-linux-x86_64', '/opt/ragout-2.0-linux-x86_64/lib')
('/opt/ragout-2.0-linux-x86_64', '/opt/ragout-2.0-linux-x86_64/lib')
Traceback (most recent call last):
File "/opt/ragout-2.0-linux-x86_64/ragout.py", line 33, in <module>
from ragout.main import main
File "/opt/ragout-2.0-linux-x86_64/ragout.py", line 33, in <module>
from ragout.main import main
ImportError: No module named main
无论出于何种原因,它都打印了两次——尽管路径是正确的——它仍然无法从本地模块导入。有任何想法吗?
我认为这是一个权限问题。请参阅 this Stack Overflow question 用户报告了一个非常相似的问题。
- 您创建了位置,因此您拥有它的权限并且 可以导入。
- 其他用户没有权限,无法从他们无法读取的文件夹中导入。
解决方案:确保所有合适的用户都在对文件夹具有至少读取权限的用户组中。