问题
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
First install
virtualenv
:pip install virtualenv
Move to the folder where the project resides make a
requirement.txt
file with all python dependencies likeopenpyxl
Start a virtual env by command
virtualenv venv --python=python3
Activate the environment by
source venv/bin/activate
Install all requirements by
pip install -r requirement.txt
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