简体   繁体   English

为什么在文件中而不是在Python控制台中执行sys.path.append时会出现ImportError?

[英]Why do I get an ImportError when executing sys.path.append in a file but not in the Python console?

I'm trying to run a script that requires a package in another directory. 我正在尝试运行一个脚本,该脚本需要另一个目录中的软件包。

This works: 这有效:

  • execfile("../test.py") when starting python in package parent directory 在包父目录中启动python时的execfile("../test.py")
  • sys.path.append("package parent") while in the python interpreter and then calling execfile("test.py") sys.path.append("package parent")在python解释器中,然后调用execfile("test.py")

This doesn't work (gives an ImportError ): 这不起作用(给出ImportError ):

  • python ../test.py when in package parent directory 在包父目录中时是python ../test.py
  • python test.py when in script directory 在脚本目录中时, python test.py

The test.py file contains the same sys.path.append instruction I used in the interpreter. test.py文件包含与解释器中相同的sys.path.append指令。 Is there a difference between running it in the file or in the interpreter? 在文件或解释器中运行它是否有区别?


My directory structure: 我的目录结构:

  • test.py test.py
  • package
    • stuff.py stuff.py

The error message I get: 我收到的错误消息:

from package.stuff import SomeClass 从package.stuff导入SomeClass
ImportError: No module named package.stuff ImportError:没有名为package.stuff的模块

A hint why the behavior from the interactive shell is different from script behavior can be found in the docs for sys.path : 可以在sys.path文档中找到有关交互式shell行为与脚本行为不同的提示:

As initialized upon program startup, the first item of this list, path[0] , is the directory containing the script that was used to invoke the Python interpreter. 在程序启动时初始化后,该列表的第一项path[0]是包含用于调用Python解释器的脚本的目录。 If the script directory is not available (eg if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. 如果脚本目录不可用(例如,如果交互式调用解释器或从标准输入中读取脚本),则path[0]为空字符串,该字符串将引导Python首先搜索当前目录中的模块。 Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH . 注意,由于PYTHONPATH的结果,脚本目录已插入到插入的条目之前。

This should explain why executing from an interactive shell works. 这应该可以解释为什么从交互式外壳执行是可行的。 However, without further information it can only be guessed why the script fails. 但是,没有更多信息,只能猜测脚本为什么失败。

Setting PYTHONPATH is essentially the same as updating sys.path . 设置PYTHONPATH本质上与更新sys.path相同。 On bash this would be: bash上将是:

PYTHONPATH=/path/to/package1:/path/to/package2 python test.py

Check out this post and the docs for details. 查看这篇文章文档以了解详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python sys.path.append - Python sys.path.append 如何删除sys.path.append(“ ../”)但无法获取ImportError:没有名为XXX的模块 - How can I delete sys.path.append(“../”) but not get ImportError: No module named XXX 为什么我需要包含 sys.path.append 来使用 Python 3.6 导入模块而我的大学不需要? - Why do I need to include sys.path.append to import a module with Python 3.6 and my colleges doesn't need? sys.path.append语句的文件 - File for sys.path.append statement 如何从我的所有导入中替换 import sys sys.path.append('./') - How do I replace import sys sys.path.append('./') from all my imports 如何在没有 sys.path.append 的 Python 中进行相对导入,既没有 -m falg 也没有 __init__.py 文件? - How to do relative imports in Python without sys.path.append neither -m falg neither with __init__.py file? 在Python中,sys.path.append('path / to / module')引发语法错误 - In Python, sys.path.append('path/to/module') throws syntax error 何时使用sys.path.append以及何时修改%PYTHONPATH%就足够了 - When to use sys.path.append and when modifying %PYTHONPATH% is enough 有没有更好的方法在 python 项目中设置 sys.path.append ? - Is there a better way to set sys.path.append in python project? Python __init__.py 与 sys.path.append/insert - Python __init__.py vs sys.path.append/insert
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM