dload
dload copied to clipboard
Cannot reproduce README example: KeyError: '__file__'
I try to run the examples from the repo's README file but I get an error. The version of dload is 0.6, python is 3.6.9 and the requests library is 2.23.0; OS is Ubuntu 18.04
import dload
dload.save('https://upload.wikimedia.org/wikipedia/commons/thumb/d/d3/Albert_Einstein_Head.jpg/800px-Albert_Einstein_Head.jpg', '~/einstein.jpg')
Traceback (most recent call last):
File "/home/nestor/.local/share/virtualenvs/dl_projects-d1owGBMD/lib/python3.6/site-packages/dload/__init__.py", line 72, in save
c_path = os.path.dirname(namespace['__file__'])
KeyError: '__file__'
None
''
For some reason, the c_path
assignment returns a different object when inside a try
block.
I'm seeing this as well
Running into the same error.
Same error running on google colab
Same here.
Running into the same error in colab environment. Is anyone running into this problem outside of an interactive environment?
Edit: _file_ is not guaranteed to be set. Hence my quick fix (not tested) provides some fallback paths:
def get_default_path():
"""
Provides call path with fallbacks
:return: str - The default save path
"""
namespace = sys._getframe(1).f_globals # caller's globals
if hasattr(namespace, '__file__'): # standard case
c_path = os.path.dirname(namespace['__file__'])
elif hasattr(globals(), '_dh'): # ipython environment
c_path = os.path.dirname(globals()['_dh'][0])
else:
c_path = os.getcwd() # fallback
return c_path
@regtm same problem for colab, the code needed some patching
I've forked the project to incorporate the path fallback and also changes for streamed downloads from @miccoh1994
@regtm LGTM, could you draft docs on how this can be installed in Colab?
@BrandonKMLee you can install it directly from the repo with !pip install git+https://github.com/regtm/dload.git