简体   繁体   English

Python 无法在工作计算机上找到已安装的模块 -Windows 操作系统

[英]Python unable to find installed modules on work computer -Windows OS

I have this python script I wrote that works fine on my computer at home, where I'm admin.我有这个我写的 python 脚本,它在我家的电脑上运行良好,我是管理员。 I've tried using it at work (uses network drive and I am not admin), and I keep running into this problem that any module I use isn't recognized.我已经尝试在工作中使用它(使用网络驱动器并且我不是管理员),并且我一直遇到这个问题,即我使用的任何模块都无法识别。

I installed each package with python -m pip install.我用 python -m pip 安装了每个 package。 When I run python from a command prompt, I am able to import and use each package;当我从命令提示符运行 python 时,我可以导入和使用每个 package; however, when I try to run code with VS code, it returns "ModuleNotFoundError: No module named '_______'.但是,当我尝试使用 VS 代码运行代码时,它返回“ModuleNotFoundError: No module named '_______'。

I've manually chosen an interpreter, and even specified the absolute path to site-packages for the "PYTHONPATH" property of "env" in the launch.json file.我手动选择了一个解释器,甚至在launch.json文件中为“env”的“PYTHONPATH”属性指定了站点包的绝对路径。 I also even tried moving the program to site-packages and it still couldn't find the packages.我什至还尝试将程序移动到站点包,但它仍然找不到包。

Let me know if there's any other info I need to include.让我知道我是否需要包含任何其他信息。 I've scoured the online sources for weeks as best I can for a similar issue, but none of the answers I've found have worked so far.我已经尽我所能搜索了几个星期的在线资源来解决类似的问题,但到目前为止我找到的答案都没有奏效。 Any help is immensely appreciated as I would really love to get this script working.非常感谢任何帮助,因为我真的很想让这个脚本工作。

# 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))

It looks like you have not selected the proper python interpreter in the VSCode.看起来您没有在 VSCode 中选择正确的 python 解释器。

While you can execute the python file successfully in the command prompt, you should have installed the packages in the global python environment.虽然您可以在命令提示符下成功执行 python 文件,但您应该已经在全局 python 环境中安装了这些包。 But you have not selected this python environment in the VSCode.但是你没有在VSCode中选择这个python环境。

You can click the python interpreter at the bottom-left on the VSCode to switch the python interpreter.您可以点击 VSCode 左下角的 python 解释器来切换 python 解释器。

在此处输入图像描述

You can execute pip show {packagename} to get where the package has been installed, pip --version to get which pip you are using, and print(sys.prefix) to get which python environment you are using. You can execute pip show {packagename} to get where the package has been installed, pip --version to get which pip you are using, and print(sys.prefix) to get which python environment you are using.

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

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