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

Module generator bug fixed to correctly import Custom files #348

Conversation

DevarshiChoudhury
Copy link
Member

No description provided.

@thjsal
Copy link
Contributor

thjsal commented Nov 12, 2023

The problem is that adding this dot in front of the imported module breaks modules if running them directly. Like this:

(xpsi_py3) tuomo@tuomo-XPS-13-9310:~/xpsi/xpsi_group/xpsi/examples/examples_module_generator$ python _auto_modules/main.py @config.ini 
/=============================================\
| X-PSI: X-ray Pulse Simulation and Inference |
|---------------------------------------------|
|                Version: 2.1.1               |
|---------------------------------------------|
|      https://xpsi-group.github.io/xpsi      |
\=============================================/

Imported GetDist version: 1.4.5
Imported nestcheck version: 0.2.1
Parsing configuration file...
--main-import-statements
...
Configuration file parsed.
Rank reporting: 0
Traceback (most recent call last):
  File "/home/tuomo/xpsi/xpsi_group/xpsi/examples/examples_module_generator/_auto_modules/main.py", line 858, in <module>
    from .CustomInstrument import CustomInstrument
ImportError: attempted relative import with no known parent package

@DevarshiChoudhury
Copy link
Member Author

DevarshiChoudhury commented Nov 13, 2023

Let me test if I face the same issue. I don't think I have directly run the main module generated using py3 module generator. I was facing import issues when importing the main module from a notebook. If I am also able to reproduce the error above, I suppose a good way would to be put a if __name__ == '__main__' condition when importing the Custom modules in main

@DevarshiChoudhury
Copy link
Member Author

@thjsal I have pushed another commit to account for relative imports.

'''.format(args.custom_background_module)
)
elif args.background_shared_class:
for _instrument in args.instruments:
module += (
'''
from {0} import {1}_CustomBackground
if __name__ == '__main__':
from .{0} import {1}_CustomBackground
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably no dot should be here, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And actually, it probably should be:

if __name__ == '__main__':
  from {0} import {1}_CustomBackground
          '''.format(args.custom_background_module,
                     _instrument)
else:
  from .{0} import {1}_CustomBackground
          '''.format(args.custom_background_module,
                     _instrument)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes that's correct. The else condition is fine in the commit. The if condition has the typo that you caught

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a commit to fix that.

@DevarshiChoudhury DevarshiChoudhury merged commit b34efb5 into main Dec 8, 2023
4 checks passed
@DevarshiChoudhury DevarshiChoudhury deleted the 347-python-3-version-needs-from-custom-to-find-the-custom-file branch December 8, 2023 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python 3 version needs from .Custom... to find the Custom file
2 participants