简体   繁体   English

为什么导入对我不起作用? - Python

[英]Why doesn't import work for me? - Python

Whenever I try to import a file into python, it comes up with this error(or similar):每当我尝试将文件导入 python 时,都会出现此错误(或类似错误):

Traceback (most recent call last):
  File "C:/Python33/My Files/username save.py", line 1, in <module>
    import keyring.py
ImportError: No module named 'keyring'

I am trying to create a password storing program, and I was looking up for good ways to keep passwords secure, and someone said use import keyring, so I did, except, it never works.我正在尝试创建一个密码存储程序,并且我正在寻找保护密码安全的好方法,有人说使用导入密钥环,所以我做了,但它从来没有用过。 I must be doing something wrong, but whenever I look anything up for python, it never works out for me.我一定是做错了什么,但是每当我为 python 查找任何东西时,它对我来说永远不会奏效。 It's almost as if loads have things have been changed over the years.就好像这些年来负载发生了变化。

and idea's?和想法?

The keyring module is not part of the Python standard library. keyring模块不是Python标准库的一部分。 You need to install it first. 您需要先安装它。 Installation instructions are included. 包括安装说明

Once installed, use import keyring , not import keyring.py ; 安装完成后,使用import keyring ,而不是import keyring.py ; the latter means import the py module from the keyring package . 后者意味着keyring包中导入py模块 Python imports should use just the name of the module, so not the filename with extension. Python导入应使用模块的名称,因此不应使用带扩展名的文件名。 Python can import code from more than just .py python files. Python可以从不仅仅是.py python文件导入代码。

I was getting the same error "ModuleNotFoundError: No module named 'keyring'".我收到了同样的错误“ModuleNotFoundError: No module named 'keyring'”。 And after installing this module pip install keyring , the same error occured with another module name.安装此模块pip install keyring后,另一个模块名称发生了同样的错误。 Then I came to the conclusion that it is the fact that the VSCode is not able to connect to my venv , even after setting the Python Itereptor .然后我得出的结论是,即使在设置Python Itereptor venv Press CTRL + SHIFT + P, and then type Python: Select Interceptor, and select your venv, if you want to set this.按 CTRL + SHIFT + P,然后键入 Python:选择拦截器,然后选择你的 venv,如果你想设置它。

在此处输入图像描述

To fix the issue, I had to force the VSCode to use the .venv I created, and luckily there is a dropdown to do that, on the top right corner as in the preceeding image.为了解决这个问题,我不得不强制 VSCode 使用我创建的.venv ,幸运的是,在右上角有一个下拉菜单可以做到这一点,如上图所示。 Click ont the Python version, and then you will be able to select your virtual environment.单击 Python 版本,然后您将能够选择您的虚拟环境。

在此处输入图像描述

Now it will take the modules from your virtual environment.现在它将从您的虚拟环境中获取模块。

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

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