简体   繁体   English

通过 bat 文件运行 Python 脚本时找不到导入的模块

[英]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.

相关问题 通过 VBA 或 .bat 打开时运行 Python 脚本 - Run Python script when opened via VBA or .bat 当 Python 脚本通过 bat 文件运行时,Outlook 中不显示图像 - Image not displayed in Outlook when Python script is run through bat file 在MATLAB中找不到或导入Python模块 - Python module import in MATLAB cannot be found or imported Python 脚本在 Pycharm 中运行,但不是通过 bat 文件 - Python script is running in Pycharm but not via bat file Bat文件可在Windows上运行Python脚本 - Bat file to run Python script on Windows 从命令行运行 python 脚本时未找到模块错误 - module not found error when python script is run from command line 尝试远程运行python脚本时“找不到模块” - “module not found” when trying to run python script remotely 从命令行运行 python 脚本时找不到模块 - module not found when python script run from command line NameError:尝试在导入的模块/脚本Python 3中运行函数时,未定义名称“ test” - NameError: name 'test' is not defined , when trying to run a function in an imported module/script Python 3 当作为脚本运行时,为什么不将源/字节代码文件作为模块导入? - Why isn’t a source/byte code file imported as a module when run as a script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM