简体   繁体   中英

Python - cannot import name

I think just the code will be enough for you to understand everything

exampleforstack.py

from psgss import login,password 

print(login,password)

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:

Imports should ideally either be in a directory listed on PYTHONPATH, if they're outside the current package. 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.

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 . (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. 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.

Try changing the import line to 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. (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.)

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:

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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