Using PIP to install openpyxl for a user

≡放荡痞女 提交于 2021-02-08 11:07:43

问题


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.

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.

I have put together a bit of code based off a few other threads below to check for openpyxl before it imports. So I can hopefully have the ability to setup openpyxl. I intend to have this bit of code run during the import of the os, sys, time, datetime etc.

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 ? So if I have the Openpyxl folder in the same directory as the script how can I point too it ? Would I need to change the sys path ?

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.


回答1:


The preferred way is to use virtualenv

  1. First install virtualenv : pip install virtualenv

  2. Move to the folder where the project resides make a requirement.txt file with all python dependencies like openpyxl

  3. Start a virtual env by command virtualenv venv --python=python3

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

  5. Install all requirements by pip install -r requirement.txt

  6. When you want to get out of the environment just use : source venv/bin/deactivate

Refer this: https://virtualenv.pypa.io/en/stable/



来源:https://stackoverflow.com/questions/48924490/using-pip-to-install-openpyxl-for-a-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!