Jump to content

Can't Import Library/Package


Recommended Posts

First of all, this is my second time writing code. It is supposed to insert a number provided by the user (in the Alfred dialog) into a predefined cell in a predefined Excel sheet. The code worked when I ran it in Terminal (python 3.7) and on Anaconda (python 3.7). When I tried running it in Terminal on usr/bin/python (python 2.7) it couldn't import the module/library (https://pypi.org/project/openpyxl/). However, I thought by downloading the package, moving it to the workflow's local folder, and running it on Alfred the issue will be solved. It didn't work and I received this error on debugging (replacing the path seen in the 4th line with PATH in the rest of the lines):

 

Quote

[19:17:00.049] ERROR: MyWorkflow[Run Script] Traceback (most recent call last):
  File "excel.py", line 8, in <module>
    from openpyxl import load_workbook
  File "/Users/Yaz/Main/Miscellaneous/Alfred/Sync Preferences/Alfred.alfredpreferences/workflows/user.workflow.13244C9F-6157-47F2-8AF9-2F7A50D84DEF/openpyxl/__init__.py", line 6, in <module>
    from openpyxl.workbook import Workbook
  File "PATH/openpyxl/workbook/__init__.py", line 4, in <module>
    from .workbook import Workbook
  File "PATH/openpyxl/workbook/workbook.py", line 7, in <module>
    from openpyxl.worksheet.worksheet import Worksheet
  File "PATH/openpyxl/worksheet/worksheet.py", line 392
    return f"{get_column_letter(min_col)}{min_row}:{get_column_letter(max_col)}{max_row}"
                                                                                        ^
SyntaxError: invalid syntax

 

 

This is the code:

#!/usr/bin/python
# encoding: utf-8

import sys
import os
from workflow import Workflow
from workflow import Variables
from openpyxl import load_workbook

v = os.getenv('questions')
path = '/Users/Yaz/Desktop/Schedule.xlsx'
wb = load_workbook(filename = path)
sheet = wb['Sheet1']
sheet['I1'].value = v
wb.save(path)

The workflow starts with a "Keyword" object that requires an argument (the number) connected to an "Arg and Vars" ,which assigns the name "questions" to the {query}, connected to a "Run Script" action (/bin/bash) with "/usr/bin/python excel.py "{var:questions}" as the script.

 

I have Alfred 4.1 running on macOS Mojave.

Edited by Yaz
To add important detail
Link to comment

Python 2 and Python 3 are not particularly compatible with each other. To get any non-trivial program to work with both takes quite a lot of effort.

 

My library does not work with Python 3, and f-strings (f"{blah...}") are not supported in Python 2.

 

You need to pick one version of Python and stick with it. At this point, Python 3 is probably the better choice.

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...