question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NameError in list comprehension when using embed

See original GitHub issue

This used to work, and stopped working somewhere around six months to a year ago.

In [51]: from os.path import getsize

In [52]: ziplen = dict((_, getsize(_)) for _ in zips)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/nrg1/tools/datateam/packages/nuance.com/nvc4-pipeline-utils/nvc4qless.py in <module>()
----> 1 ziplen = dict((_, getsize(_)) for _ in zips)

/nrg1/tools/datateam/packages/nuance.com/nvc4-pipeline-utils/nvc4qless.py in <genexpr>((_,))
----> 1 ziplen = dict((_, getsize(_)) for _ in zips)

NameError: global name 'getsize' is not defined

zips is a list of paths, but this happens for list comprehension all over the place. Not sure what local/global namespace the list comprehension doesn’t get.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mathieulongtincommented, Oct 21, 2015

Ok, start_ipython(argv=[]) is more elegant.

0reactions
mathieulongtincommented, Oct 21, 2015

Deep diving in the code yields this:

embed(module=sys.modules['__main__'], user_ns=sys.modules['__main__'].__dict__)

That way, globals and locals are the same object, like in the normal shell.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does list comprehension closure throw `NameError` in ...
A list comprehension in a class definition can reference other class level variables in both python 2 and 3 as part of the...
Read more >
Comprehensions - The Conservative Python 3 Porting Guide
List comprehensions, a shortcut for creating lists, have been in Python since version 2.0 ... as running the code under Python 3 will...
Read more >
list comprehensions don't see local variables in pdb in python3
The NameError exception occuring on a generator expression referencing a local variable when the generator is called within exec() is the object ......
Read more >
List comprehension - NameError: name '_[1]' is not defined ?
mini-template, i'd like to embed other expressions for the % operator, and that may of course also be list comprehensions. Thanks!
Read more >
Can you embed a try in a Python list comprehension?
No, you can not do this directly in a list comprehension. You must factor the try/except logic out into a separate function, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found