[英]Imported module cannot be found when Python script is run via a bat file
I know there has been similar problems, but unfortunately most of them are related to errors with pyperclip itself instead of the batch file, which i suspect is where the problem stems from.我知道有类似的问题,但不幸的是,它们中的大多数都与 pyperclip 本身而不是批处理文件的错误有关,我怀疑这是问题的根源。 Below is an MRE of my Python script:
下面是我的 Python 脚本的 MRE:
#! python3 -> Do I have to use my version(3.8)?
# pw.py - An insecure password locker program.
import sys, pyperclip
#do something with the module
And my batch file pw.bat:还有我的批处理文件 pw.bat:
@py.exe C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py %*
@pause
I am running python 3.8 on windows 10. I imported the pyperclip module in my python script pw.py and ran the file via pw.bat, and this in turn gives me this error:我在 windows 10 上运行 python 3.8。我在我的 python 脚本 pw.py 中导入了 pyperclip 模块,然后又通过 pw.py 运行了该文件:
Traceback (most recent call last):
File "C:\Users\KEVIN\PycharmProjects\atbs_exercise\pw.py", line 7, in <module>
import sys, pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Press any key to continue . . .
Which shouldn't happen as I have installed pyperclip on the project using pip, and the script itself runs just fine in pycharm.这不应该发生,因为我已经使用 pip 在项目上安装了 pyperclip,并且脚本本身在 pycharm 中运行得很好。 What am I missing?
我错过了什么?
EDIT: I forgot to mention that I am using pycharm.编辑:我忘了提到我正在使用 pycharm。 So the thing is that pycharm had also installed python.exe in the project folder.
所以问题是 pycharm 也在项目文件夹中安装了 python.exe。 And as the module pyperclip is only installed to that folder, the python.exe used in the bat must point to the one in the project folder.
并且由于模块pyperclip只安装到那个文件夹,所以bat中使用的python.exe必须指向项目文件夹中的那个。
i don't know why are you using py.exe.我不知道你为什么使用 py.exe。 when running commands from a batch file or cmd.you should use
python.exe
.obviously you would need to add python to add for doing so.instead of adding py.exe to path,add python in system variable Path
which is somewhere present in C:\Users\[username]\AppData\Local\Programs\Python\
(your path might be diffrernt).you can add python in Path by following this post when running commands from a batch file or cmd.you should use
python.exe
.obviously you would need to add python to add for doing so.instead of adding py.exe to path,add python in system variable Path
which is somewhere present in C:\Users\[username]\AppData\Local\Programs\Python\
(您的路径可能不同)。您可以按照此帖子在路径中添加 python
after adding python to path just use the following batch-file:将 python 添加到路径后,只需使用以下批处理文件:
@echo off
python path-to-your-py-file\filename.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.