推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
c00WKmdje2wZLrSI
V2EX  ›  Python

一个 Python import 模块失败的问题

  •  
  •   c00WKmdje2wZLrSI · Oct 12, 2018 · 4861 views
    This topic created in 2959 days ago, the information mentioned may be changed or developed.

    代码如下:

    import os
    
    try:
        import winshell
    except:
        os.system('python -m pip  install --upgrade pip')
        os.system('pip install pywin32')
        os.system('pip install winshell')
        import winshell
    
    print('hello world')
    

    我的本意是导入 winshell 这个模块,要是机子里没这个模块的话,就先安装再导入,但是运行的时候报错了

    图片

    看运行过程,明明的确按计划安装了两个所需的模块,但是接下来又报错说没有 winshell 模块,这是怎么回事?

    第二遍运行的话又没问题,说明第一遍的确安装成功了两个模块,但是第一遍的报错是怎么一回事呢?

    Supplement 1  ·  Oct 12, 2018

    但是我后来又用这种方法试了requests模块,又没有问题,再试winshell,还是报错,不知道问题在哪

    图片

    17 replies  •  2018-10-13 17:14:38 +08:00
    raycool
    raycool
    Oct 12, 2018
    1
    我的理解:
    python 安装后是不是还要更新类似索引等操作。
    而安装成功后直接就调用了,根本没等索引什么的重建完成
    故报错
    shallyy
    shallyy
    Oct 12, 2018
    2
    试试把 site-packages 的路径加入到 PYTHONPATH 环境变量
    beny2mor
    beny2mor
    Oct 12, 2018
    3
    用了 anaconda 之后就再也没碰到这些问题了
    你可以考虑下用类似的工具
    c00WKmdje2wZLrSI
    c00WKmdje2wZLrSI
    OP
    Oct 12, 2018
    4
    @raycool
    神奇之处在于,按这样子做,requests 模块又没关系
    ![图片]( https://i.loli.net/2018/10/12/5bc0629d140c9.png)
    c00WKmdje2wZLrSI
    c00WKmdje2wZLrSI
    OP
    Oct 12, 2018
    5
    @shallyy 但是 requests 模块这样子的话又没问题
    c00WKmdje2wZLrSI
    c00WKmdje2wZLrSI
    OP
    Oct 12, 2018
    6
    @beny2mor 我的问题是为什么第一遍运行的时候会出问题,但是第二遍就没问题了
    beny2mor
    beny2mor
    Oct 12, 2018
    7
    明白。。
    在 python 脚本里运行 pip 安装包有点奇怪。
    应该和 python 的加载机制有关。你可以查下文档,https://docs.python.org/3/reference/import.html#searching
    0ZXYDDu796nVCFxq
    0ZXYDDu796nVCFxq
    Oct 12, 2018 via Android
    8
    这个不是运维的事吗,为啥要在代码里写这些东西……

    requirements.txt 了解一下
    lniwn
    lniwn
    Oct 12, 2018
    9
    把第二个 import winshell 换成 subprocess.Popen(['python.exe', os.path.realpath(__file__)], creationflags=0x00000008)试试
    c00WKmdje2wZLrSI
    c00WKmdje2wZLrSI
    OP
    Oct 12, 2018
    10
    @lniwn 主要是这种方法装 requests 模块又没问题
    c00WKmdje2wZLrSI
    c00WKmdje2wZLrSI
    OP
    Oct 12, 2018
    11
    @beny2mor 主要是这种方法装 requests 模块又没问题
    beny2mor
    beny2mor
    Oct 12, 2018
    12
    @c00WKmdje2wZLrSI 你这报错说`winshell`找不到`wincon`,真要搞明白就得去了解下 python 模块加载机制了。。。
    还是不要纠结这个问题了,就在运行 python 脚本前先安装了依赖。。
    浪费时间在这个地方没意义,非常规操作....
    pabupa
    pabupa
    Oct 12, 2018 via Android
    13
    win32 什么的应该还是 dll 吧,,requests 和它的依赖都是纯 Python 的,可能是这点区别,加载机制不一样吧……
    zyqf
    zyqf
    Oct 13, 2018 via Android
    14
    使用 sys.path.insert 重新添加环境目录
    swordspoet
    swordspoet
    Oct 13, 2018 via iPhone
    15
    第一次执行脚本,脚本安装 package,当执行到 import 语句,刚刚安装好的 package 还没有被索引到。我猜是这个原因。
    publicvoid
    publicvoid
    Oct 13, 2018
    16
    13 楼很有可能是正解, 楼主这种情况是 request 不需要依赖 dll,winshell 肯定依赖 windows,话说什么场景为要这样动态导入?
    filosfino
    filosfino
    Oct 13, 2018
    17
    reload sys.modules
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   976 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 22:04 · PVG 06:04 · LAX 14:04 · JFK 17:04
    ♥ Do have faith in what you're doing.