简体   繁体   English

使用PIP为用户安装openpyxl

[英]Using PIP to install openpyxl for a user

I've written a script that will enable a user to have their weekly timesheet's filled out automatically and save it as a new excel spreadsheet. 我编写了一个脚本,该脚本将使用户能够自动填写其每周时间表并将其另存为新的Excel电子表格。

I thought I would be able to just have the Openpyxl folder in the same directory as the script for it to import but it does not seem to work. 我以为我可以将Openpyxl文件夹与脚本导入到相同的目录,但是它似乎不起作用。

I have put together a bit of code based off a few other threads below to check for openpyxl before it imports. 我根据下面的一些其他线程整理了一些代码,以在导入之前检查openpyxl。 So I can hopefully have the ability to setup openpyxl. 因此,我希望可以设置openpyxl。 I intend to have this bit of code run during the import of the os, sys, time, datetime etc. 我打算在导入os,sys,time,datetime等过程中运行这段代码。

try:
    __import__('imp').find_module('openpyx')
    print('this worked')
except ImportError:
    pass
    print('')
    print('You do not have OpenpyXl installed - it will now install')
    print('')

I understand I should be using PIP ? 我了解我应该使用PIP吗? So if I have the Openpyxl folder in the same directory as the script how can I point too it ? 所以,如果我在脚本所在的目录中有Openpyxl文件夹,该如何指向呢? Would I need to change the sys path ? 我需要更改sys路径吗?

This is how my code essentially starts out - 这就是我的代码从本质上开始的方式-

#!/usr/bin/env python3
import os
import sys
import openpyxl
import time
from datetime import datetime
from datetime import timedelta

FMT = '%H:%M'

print("")
print("Welcome to Timesheet Bot 1.0")
print ('\033[91m' + "I now will ask you a few questions about your work week." + '\033[0m')
time.sleep(3)

# Location of spreadsheet

file = str(input('Please drag and drop the spreadsheet here : '))
path = file
os.chdir(path)
wb = openpyxl.load_workbook('DC.xlsx')
FMT = '%H:%M'
sheet = wb['TIMESHEET']

# Name of the individual
print("")
name = str(input('What is your full name? '))
sheet['B9'] = name

Im still learning as I go so appreciate any guidance or reading that I should do to learn it. 我仍在学习中,非常感谢我为学习它而进行的任何指导或阅读。

The preferred way is to use virtualenv 首选方法是使用virtualenv

  1. First install virtualenv : pip install virtualenv 首先安装virtualenvpip install virtualenv

  2. Move to the folder where the project resides make a requirement.txt file with all python dependencies like openpyxl 移动到该项目所在做的文件夹requirement.txt文件,所有的Python的依赖就像openpyxl

  3. Start a virtual env by command virtualenv venv --python=python3 通过命令virtualenv venv --python=python3启动虚拟环境

  4. Activate the environment by source venv/bin/activate 通过source venv/bin/activate

  5. Install all requirements by pip install -r requirement.txt 通过pip install -r requirement.txt所有pip install -r requirement.txt

  6. When you want to get out of the environment just use : source venv/bin/deactivate 当您想离开环境时,只需使用: source venv/bin/deactivate

Refer this: https://virtualenv.pypa.io/en/stable/ 请参阅此: https : //virtualenv.pypa.io/en/stable/

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

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