python 导入模块错误

python 导入模块错误

每次运行我的脚本我都会得到:

  File "./ciinstall.py", line 35, in <module>
  import npyscreen
ImportError: No module named npyscreen

这是我的代码。它检查 mypyscreen 是否已安装。如果没有,则安装它,然后导入。如果已安装,则导入。但是它却给了我一个错误。

# Installing npyscreen before importing npyscreen for menu UI
npy = subprocess.call(shlex.split('locate npyscreen-3.2.egg-info'))
if npy == '1':
    os.chdir('/opt/')
    subprocess.call(shlex.split('sudo wget https://pypi.python.org/packages/sou$
    subprocess.call(shlex.split('tar xvf npyscreen-3.37.tar.gz'))
    os.chdir('npyscreen-3.2')
    subprocess.call(shlex.split('sudo ./setup.py'))
    subprocess.call(shlex.split('sudo rm npyscreen-3.37.tar.gz'))

import npyscreen

答案1

根据 Windows 上的 7-Zip,存档中有一个 Setup.py,但没有 3.2 目录,我在根目录中有 /setup.py,而且,设置的路径是:

\npyscreen-3.37.tar.gz\dist\npyscreen-3.37.tar\npyscreen-3.37\setup.py

os.chdir('/opt/'), puts you in /opt
wget https://pypi.python.org/packages/source/n/npyscreen/npyscreen-3.37.tar.gz, would download the tar into /opt
tar xvf npyscreen-3.37.tar.gz, should untar the package, in /opt
os.chdir('npyscreen-3.2') should be:
os.chdir('npyscreen-3.37')

然后:

sudo ./setup.py
enter code here
sudo rm npyscreen-3.37.tar.gz
echo "Download and Install Finished."
import npyscreen

答案2

我不喜欢在没有告诉用户的情况下安装软件包,但我应该这样做:

try:
    import npyscreen
except ImportError:
    # Package not found, install it
    import subprocess
    subprocess.call(["sudo", "pip", "install", "npyscreen"])
    #TODO: check the return code for errors
    import npyscreen

相关内容