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

%run magic does not use __main__.py when called on module path #12081

Open
astromancer opened this issue Jan 10, 2020 · 1 comment
Open

%run magic does not use __main__.py when called on module path #12081

astromancer opened this issue Jan 10, 2020 · 1 comment

Comments

@astromancer
Copy link

Here's a small demo package:

test_main/
├── __init__.py
└── __main__.py

With __init__.py having one line of code:

print('ran __init__.py')

and __main__.py:

print('ran __main__.py')
variable = 1

Here are a few example that show how this fails:
In [1]: !ls test_main/
__init__.py __main__.py

In [2]: import test_main
ran __init__.py

In [3]: %run test_main
ERROR:root:File 'test_main.py' not found.

Using the -m switch seems to work however, the one does not get access to the module namespace like you would when running a regular script. I.e the variable above remains undefined, which is undesirable for interactive work.
In [4]: %run -m test_main
ran __main__.py

In [4]: variable

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-1748287bc46a> in <module>
----> 1 variable

NameError: name 'variable' is not defined

To get access to the namespace one has to provide the full path.
%run test_main/__main__.py
ran __main__.py
In [9]: variable
Out[9]: 1

Wondering if it would be possible to get the __main__.py script to run when calling the %run on the module path?

@astromancer
Copy link
Author

related to #9812

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

1 participant