Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot reproduce README example: KeyError: '__file__' #4

Open
nestorSag opened this issue Jul 15, 2020 · 10 comments
Open

Cannot reproduce README example: KeyError: '__file__' #4

nestorSag opened this issue Jul 15, 2020 · 10 comments

Comments

@nestorSag
Copy link

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
''
@max-santiago
Copy link

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

@aribornstein
Copy link

I'm seeing this as well

@jrieke
Copy link

jrieke commented Nov 26, 2020

Running into the same error.

@Daniel-Trung-Nguyen
Copy link

Same error running on google colab

@jloveric
Copy link

Same here.

@regtm
Copy link

regtm commented Sep 18, 2022

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

@BradKML
Copy link

BradKML commented Sep 23, 2022

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

@regtm
Copy link

regtm commented Oct 6, 2022

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

@BradKML
Copy link

BradKML commented Oct 6, 2022

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

@regtm
Copy link

regtm commented Oct 6, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants