简体   繁体   English

Python - 无法导入名称

[英]Python - cannot import name

I think just the code will be enough for you to understand everything我认为只要代码就足以让你理解一切

exampleforstack.py堆栈示例.py

from psgss import login,password 

print(login,password)

psgss.py psgss.py

login = "login"
password = 33

and here is the error这是错误

Traceback (most recent call last):
  File "b:\vse parsers\exampleforstack.py", line 4, in <module>
    from psgss import login,password
ImportError: cannot import name 'login' from 'psgss' (b:\vse parsers\psgss.py)

idk what to do, this error appears every time i try to import any files.知道该怎么做,每次尝试导入任何文件时都会出现此错误。 Modules are imported without errors, problems appear only with files, thanks in advance模块导入没有错误,问题只出现在文件中,提前致谢

I see a couple potential problems, depending on your setting of PYTHONPATH, what other files are present, and details of your invocation:我看到了一些潜在的问题,具体取决于您对 PYTHONPATH 的设置、存在的其他文件以及调用的详细信息:

Imports should ideally either be in a directory listed on PYTHONPATH, if they're outside the current package.理想情况下,导入应该位于 PYTHONPATH 上列出的目录中,如果它们在当前包之外。 This probably isn't directly relevant to you though, since you're likely thinking of psgss.py and exampleforstack.py as parts of the same package.不过,这可能与您没有直接关系,因为您可能将psgss.pyexampleforstack.py视为同一包的一部分。

If you're not importing from something outside the current package, ie something on the PYTHONPATH, it should be in a directory with an __init__.py .如果你不是从当前包之外的东西导入,即 PYTHONPATH 上的东西,它应该在一个带有__init__.py的目录中。 (IIRC, __init__.py is no longer strictly required, but you still see it a lot, so try adding it if you don't have it.) Adding __init__.py is unlikely to be enough by itself, though. (IIRC, __init__.py init__.py 不再是严格要求的,但您仍然经常看到它,所以如果您没有它,请尝试添加它。)不过,仅添加__init__.py不太可能足够。 It effectively says psgss.py and exampleforstack.py are part of the same package, but that's not what you're conveying in your imports as written.它有效地表示psgss.pyexampleforstack.py是同一个包的一部分,但这不是您在导入时所传达的内容。

Try changing the import line to from .psgss import login, password .尝试将导入行更改为from .psgss import login, password The period before "psgss" is critical, since it tells Python to look for the file / module in the same directory as the invoking code. "psgss" 之前的句点很重要,因为它告诉 Python 在调用代码相同的目录中查找文件/模块。 (This is a difference in "relative imports" versus "absolute imports", and changed in Python 2 -> Python 3. Python 2 tried to be much more forgiving of such omissions, but would occasionally import from the PYTHONPATH when an import from the local directory was intended. Python 3 is more strict, to avoid importing global packages when local ones are intended - or vice versa.) (这是“相对导入”与“绝对导入”的区别,并且在 Python 2 -> Python 3 中发生了变化。Python 2 试图更加宽容这些遗漏,但是当从 PYTHONPATH 导入时偶尔会从 PYTHONPATH 导入本地目录。Python 3 更严格,以避免在本地包时导入全局包 - 反之亦然。)

However, you're likely still not done, depending on where you are on the command-line when doing the import.但是,您可能仍未完成,具体取决于您在执行导入时在命令行上的位置。 Python expects you to be outside that "local package" at invocation, so the expected file structure is something like this: Python 期望您在调用时位于“本地包”之外,因此预期的文件结构是这样的:

package_support/
   +------entrypoint.py
   +------package/
             +-------__init__.py
             +-------exampleforstack.py
             +-------psgss.py

In exampleforstack.py you would have from .psgss import login, password , but you would run the code from the package_support/ directory, and entrypoint.py would have a line like import package.exampleforstack which would load exampleforstack.py , which would load psgss.py .exampleforstack.pyfrom .psgss import login, password ,但您将从package_support/目录中运行代码,并且entrypoint.py将有一行类似于import package.exampleforstack的行,它将加载exampleforstack.py ,它将加载psgss.py Other things that go in package_support/ would be a README, non-Python scripts, and possibly code / directories specifically for packaging & deploying the package/ contents. package_support/其他内容将是自述文件、非 Python 脚本,以及可能专门用于打包和部署package/内容的代码/目录。

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

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