简体   繁体   中英

Can't import local module

I installed an application in the /opt directory and added its root to the PATH (for all users who want to use it). Now, when I invoke the master script from my user, it works fine, but other users report the same error:

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

Here is the master script:

#!/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())

I figured the script could face some problems expanding ragout_root and lib_absolute , so I added print(ragout_root, lib_absolute) just before the from ragout.main import main to see, what's happening. Now, when I run the app from my user, I get this:

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
...

and the users get this

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

For whatever reason it prints twice and – although the paths are correct – it still can't import from the local module. Any ideas?

I think this is a permissions issue. See this Stack Overflow question where the user reports a very similar issue.

  • You created the location, therefore you have permissions for it and can import it.
  • The other users do not have permission and cannot import from a folder they can't read.

Solution: make sure the all appropriate users are in a user group with at least read permission to the folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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