nk9 / get_dropbox_link

Code to get the URL of a file in the Dropbox folder
MIT License
6 stars 1 forks source link

Failing on dropbox module import on command line #15

Closed skotzko closed 8 months ago

skotzko commented 8 months ago

I'm trying to install via the directions in the README and keep hitting this error. I am not sure what I'm doing wrong. It's been years since I mucked around with Python so I'm very out of date on it, but I'm currently using pyenv + pipenv.

I've moved the script to $HOME/bin~ and chmod +x 'd it. This directory is in first my $PATH.

When I try to execute it in terminal, I get this module not found error.

Traceback (most recent call last): File "/Users/andrew/bin/get_dropbox_link.py", line 41, in <module> import dropbox ModuleNotFoundError: No module named 'dropbox'

I'm currently using Python 3.12.0a7 globally, managed by pyenv.

When I run python -m pip install dropbox and the urrllib install, they appear to succeed, but then when I try to run the script from the CLI, I get the above error.

nk9 commented 8 months ago

I'm not entirely sure what the problem is, but I would suggest that using pyenv could lead to issues unless you are careful with how you invoke the script within Automator. This isn't the hurdle you're hitting now, but this is the same error you'll see if you don't install dropbox somewhere that Automator can find it.

The first thing I would do is get the PYTHONPATH when it's failing:

import sys

print(sys.executable)
print(sys.path)

I'd also check that dropbox was successfully installed via python -m pip list. These will both give you clues, and help ensure that the place you've installed the module will work both on the command line and from Automator. Depending on exactly how you invoke the script, you may need to install dropbox twice so it can work in both the CLI and Automator environments, so don't hesitate to install it again if needed.

skotzko commented 8 months ago

I see dropbox installed when I run check from CLI, but then when I check from within the script itself, it is not there. It also looks like the python version (Xcode one) is not the pyenv managed one.

Here's what I got when running the script from CLI with debug statements added:

/Users/andrew  > python -m pip list
Package            Version
------------------ ---------
certifi            2023.7.22
charset-normalizer 3.3.0
dropbox            11.36.2
idna               3.4
pip                23.3
ply                3.11
requests           2.31.0
setuptools         65.5.0
six                1.16.0
stone              3.3.1
urllib3            1.26.6
/Users/andrew  >
/Users/andrew  >
/Users/andrew  > get_dropbox_link.py ~/Dropbox/Public/clouds_from_plane.jpg
EXECUTABLE:
/Applications/Xcode.app/Contents/Developer/usr/bin/python3

PATH
['/Users/andrew/bin', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python39.zip', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/lib-dynload', '/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages']

PIP LIST:
Package    Version
---------- -------
altgraph   0.17.2
future     0.18.2
macholib   1.15.2
pip        21.2.4
setuptools 58.0.4
six        1.15.0
wheel      0.37.0

Traceback (most recent call last):
  File "/Users/andrew/bin/get_dropbox_link.py", line 61, in <module>
    import dropbox
ModuleNotFoundError: No module named 'dropbox'
nk9 commented 8 months ago

Ah, what happens after you do this?

/usr/bin/python3 -m pip install dropbox

Due to an earlier issue with the way Automator invokes the script, I changed the form of the shebang to be more explicit. But that could be a problem with your use of pyenv.

skotzko commented 8 months ago

That worked! Thank you.

skotzko commented 8 months ago

Also, after I got it working, I thought it would be sweet to have this available as an Alfred file action via keyboard only, to not even have to open Finder to get the link. I followed the instructions here.

Using the script as a workflow via Alfred file action looks like this:

image

Exported the workflow here, which is just this:

image

Middle step script is just:

query=$1
get_dropbox_link.py $query
nk9 commented 8 months ago

That's great! Might be faster than the notoriously sluggish Automator, too. Thanks for this, I'll update the docs with the new dependencies and Automator info.