推荐学习书目
› 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
lucky233
V2EX  ›  Python

os.system()报错 ModuleNotFoundError 求大神指导

  •  
  •   lucky233 · Dec 11, 2020 · 3078 views
    This topic created in 2154 days ago, the information mentioned may be changed or developed.
    创建了一个项目,三个文件 a,b,c 都在一个目录下

    文件 a.py:
    def abc():
    print('这是 a 文件')

    文件 b.py
    from a import abc
    abc()

    文件 c.py
    import os
    os.system('python b.py')

    单独运行 b.py 不会报错,会去执行 abc 方法
    但是如果运行 c 文件就会报错
    Traceback (most recent call last):
    File "b.py", line 1, in <module>
    from a import abc
    ModuleNotFoundError: No module named 'a'
    请问有大神能讲解一下原因吗。。。
    2 replies  •  2020-12-11 11:39:07 +08:00
    Osk
    Osk
    Dec 11, 2020 via Android❤️1
    1
    请注意 sys.path
    或 os.system 时的子进程 cwd

    每个文件中 import sys; print(sys.path) 和 os.gwtcwd() 看看
    MerlinShieh
    MerlinShieh
    Dec 11, 2020
    2
    这种很可能是由于路径变量的原因,通常是 pycharm 出现的问题,你在你的目录树上,右键 package 选择 Mark Directory as Sources Root,把需要用的文件夹添加进源码根目录,这样就可以了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5178 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 06:37 · PVG 14:37 · LAX 22:37 · JFK 01:37
    ♥ Do have faith in what you're doing.