dload icon indicating copy to clipboard operation
dload copied to clipboard

Cannot reproduce README example: KeyError: '__file__'

Open nestorSag opened this issue 4 years ago • 10 comments

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
''

nestorSag avatar Jul 15 '20 13:07 nestorSag

For some reason, the c_path assignment returns a different object when inside a try block.

max-santiago avatar Sep 26 '20 02:09 max-santiago

I'm seeing this as well

aribornstein avatar Nov 20 '20 22:11 aribornstein

Running into the same error.

jrieke avatar Nov 26 '20 22:11 jrieke

Same error running on google colab

Daniel-Trung-Nguyen avatar Jun 02 '21 01:06 Daniel-Trung-Nguyen

Same here.

jloveric avatar Aug 23 '22 21:08 jloveric

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 avatar Sep 18 '22 16:09 regtm

@regtm same problem for colab, the code needed some patching

BradKML avatar Sep 23 '22 01:09 BradKML

I've forked the project to incorporate the path fallback and also changes for streamed downloads from @miccoh1994

regtm avatar Oct 06 '22 07:10 regtm

@regtm LGTM, could you draft docs on how this can be installed in Colab?

BradKML avatar Oct 06 '22 08:10 BradKML

@BrandonKMLee you can install it directly from the repo with !pip install git+https://github.com/regtm/dload.git

regtm avatar Oct 06 '22 08:10 regtm