Python无法在工作计算机上找到已安装的模块-Windows OS
Posted
技术标签:
【中文标题】Python无法在工作计算机上找到已安装的模块-Windows OS【英文标题】:Python unable to find installed modules on work computer -Windows OS 【发布时间】:2022-01-22 11:55:48 【问题描述】:我有一个我写的 Python 脚本,它在我作为管理员的家里的电脑上运行良好。我试过在工作中使用它(使用网络驱动器,我不是管理员),我一直遇到这个问题,我使用的任何模块都无法识别。
我使用 python -m pip install 安装了每个包。当我从命令提示符运行 python 时,我可以导入和使用每个包;但是,当我尝试使用 VS 代码运行代码时,它返回“ModuleNotFoundError: No module named '_______'。
我手动选择了一个解释器,甚至在 launch.json 文件中为“env”的“PYTHONPATH”属性指定了站点包的绝对路径。我什至还尝试将程序移动到站点包,但仍然找不到包。
如果我需要包含任何其他信息,请告诉我。我已经尽我所能搜索了几个星期的在线资源来寻找类似的问题,但到目前为止我找到的答案都没有奏效。非常感谢任何帮助,因为我真的很想让这个脚本工作。
# MMDG RECEIPT RENAMER
# Author: RillienCot, CrookedKaptain
# 11/2021
import os
from io import StringIO
import datetime
from PyPDF2 import PdfFileWriter, PdfFileReader
import win32com.client as wcom
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
#Directory Definitions
unpBatchDir = r'LOCATION OF UNPROCESSED BATCHES'
pBatchDir = r'LOCATION OF PROCESSED BATCHES'
indivDir = r'FINAL LOCATION OF ALL INDIVIDUAL RECEIPTS'
#Dictionary for Fund IDs to Fund Short Formats
fundDict =
"1 GenOps": "AA",
"2 Parkinson": "DfPD"
#Loop through all .doc files in directory
#Save all found files as PDFs, remove word file
for file in os.listdir(unpBatchDir):
if file.endswith(".doc"):
filepath = os.path.join(unpBatchDir, file)
file = file.replace('.doc', '')
newFilePath = os.path.join(unpBatchDir, file + '.pdf') #Create new pdf doc with same name as word doc
#Start Word and open .doc
word = wcom.Dispatch('Word.Application')
doc = word.Documents.Open(filepath)
#Save .doc as PDF
doc.SaveAs(newFilePath, FileFormat = 17)
#Close document, exit word, and remove .doc file
doc.Close()
word.Quit()
os.remove(filepath)
#Split PDF Batch in single page PDFs, store split pages in new indivDir, move batch to processed folder
for file in os.listdir(unpBatchDir):
#iterate through each pdf file in the directory
if file.endswith(".pdf"):
filePath = os.path.join(unpBatchDir, file)
batchPDF = PdfFileReader(filePath)
#Split pages from source PDF
for pageNum in range(batchPDF.numPages):
pdfWriter = PdfFileWriter()
indivFileNaming = "/Receipt #.pdf".format(indivDir,pageNum)
pdfWriter.addPage(batchPDF.getPage(pageNum))
#Save split pages to indivDir
with open(indivFileNaming, 'wb') as indivFile:
pdfWriter.write(indivFile)
#Move batch to processed folder
os.rename(filePath, os.path.join(pBatchDir, file))
#loop through each filename found in the directory location
#rename the file
for filename in os.listdir(indivDir):
#add all pdf filenames to the filenames list
# loop through all the filenames to open each one
if filename.endswith(".pdf"):
path = os.path.join(indivDir, filename)
#Extract text from file
output_string = StringIO()
with open(path, 'rb') as in_file:
parser = PDFParser(in_file)
doc = PDFDocument(parser)
rsrcmgr = PDFResourceManager()
device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(doc):
interpreter.process_page(page)
textList = output_string.getvalue().splitlines()
for line in textList:
if line == "":
textList.remove(line)
#get name
name = textList[0].strip()
#get date and convert to "MMDDYY" format
date = textList[1]
dateObject = datetime.datetime.strptime(textList[1].strip(), "%B %d, %Y")
date = dateObject.strftime("%m%d%y")
#Get Fund ID and convert to short format for naming
fund = fundDict[textList[2].strip()]
#Name and Path for New File
newFilename = fund + " - " + name + " - " + date + ".pdf"
os.rename(path, os.path.join(indivDir, newFilename))
【问题讨论】:
【参考方案1】:您似乎没有在 VSCode 中选择正确的 Python 解释器。
虽然您可以在命令提示符下成功执行 python 文件,但您应该已经在全局 python 环境中安装了这些包。但是你在VSCode中没有选择这个python环境。
您可以点击VSCode左下角的python解释器来切换python解释器。
你可以执行pip show packagename
获取包的安装位置,pip --version
获取你正在使用的pip,print(sys.prefix)
获取你正在使用的python环境。
【讨论】:
以上是关于Python无法在工作计算机上找到已安装的模块-Windows OS的主要内容,如果未能解决你的问题,请参考以下文章
安装程序无法继续,因为你的计算机上安装了更新的Internet Explorer版本。怎么弄