在.py文件中导入模块pyperclip,在windows命令提示符下运行时显示ModuleNotFoundError

2024-10-03 02:42:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python非常陌生,对于在我熟悉的vs代码终端之外的任何地方执行python代码也完全陌生

我正在阅读阿尔·斯维加特(AlSweigart)的《用Python自动化无聊的东西》一书,我正在阅读第9章,其中有一个创建多剪辑板的后续项目

基本上,该项目正在创建一个程序,该程序使用shelve、sys和pyperclip模块将剪贴板中的文本以字典格式存储在外部文件中。书中对此进行了解释:

The program will save each piece of clipboard text under a keyword. For example, when you run py mcb.pyw save spam, the current contents of the clipboard will be saved with the keyword spam. This text can later be loaded to the clipboard again by running py mcb.pyw spam. And if the user forgets what keywords they have, they can run py mcb.pyw list to copy a list of all keywords to the clipboard.

Here’s what the program does:

The command line argument for the keyword is checked. If the argument is save, then the clipboard contents are saved to the keyword. If the argument is list, then all the keywords are copied to the clipboard. Otherwise, the text for the keyword is copied to the clipboard.

我对这个完全陌生,所以从命令提示符执行有点吓人。我所做的是在命令提示符下使用cd进行导航。然后我输入'py.exe mcb226.pyw'(我给我的命名略有不同)。该文件确实在运行,但我(在命令提示符下)出现以下错误:

Traceback (most recent call last):
   File "mcb226.pyw", line 10, in <module>
      import pyperclip
ModuleNotFoundError: No module named 'pyperclip'

这让我很困惑,因为。这到底是什么意思?我已经安装了pyperclip(我通过Anaconda提示符系统安装了pyperclip,我通过Anaconda运行vs代码)。我必须通过命令提示符安装它吗

以下是我在mcb226.pyw文件中的代码:

#! python3
# mcb.pyw - Saves and loads pieces of text to the clipboard
# usage: py.exe mcb226.pyw save <keyword> - Saves clipboard to keyword
#        py.exe mcb226.pyw <keyword> - Loads keyword to clipboard
#        py.exe mcb226.pyw list - loads all keywords to clipboard

import shelve
import sys
import pyperclip

path = 'C:/Users/adamt/Desktop/testfolder/mcb/mcb'
mcbShelf = shelve.open(path)
if len(sys.argv) == 3 and sys.argv[1].lower() == 'save':
    mcbShelf[sys.argv[2]] = pyperclip.paste()
elif len(sys.argv) == 2:
    # if the sys. argument list is 2, this must either be 'list' or to ask to get a variable, since its only mcb226.pyw list or mcb225.pyw spam
    if sys.argv[1].lower() == 'list':
        pyperclip.copy(str(list(mcbShelf.keys())))
    elif sys.argv[2] in mcbShelf:
        pyperclip.copy(mcbShelf[sys.argv[1]])

mcbShelf.close()

Tags: thetopyissavesyskeywordmcb
1条回答
网友
1楼 · 发布于 2024-10-03 02:42:57

pyperclip应该安装在运行代码的环境中pyperclip包未安装在用于运行脚本的python安装的site-packages目录中

要解决此问题,请在运行代码的终端上运行以下命令

pip install pyperclip

相关问题 更多 >