![]() |
using import with filename starting by a number followed by underscore - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: using import with filename starting by a number followed by underscore (/thread-25529.html) |
using import with filename starting by a number followed by underscore - SpongeB0B - Apr-02-2020 Hi everyone, I have a file who is named 16_module.py I would like to use some function from it. So i use import within my current script file import 16_modulebut this is not working as python see it as a number with an ending thousand separator ![]() so I use instead importlib.import_module('16_module')The import seem working but I cannot call it later on print(16_module.myfunction()) same problem here the thoushand separator..I tried importlib.import_module('16_module') as testbut this is not accepting the AS .... any ideas ? RE: using import with filename starting by a number followed by underscore - buran - Apr-02-2020 (Apr-02-2020, 05:12 AM)SpongeB0B Wrote: any ideas ?rename your module. 16_module isn't terrific descriptive name. I would have given you the same advise even if the import worked :-) as stated in the docs: Quote:Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9. RE: using import with filename starting by a number followed by underscore - snippsat - Apr-02-2020 Also you don't need to use importlib. Just a better understanding of how module and package work,i gave you some link in you previous Thread. So as a demo can make a simple package,where i lift the sub-model on level up. my_pack\ |-- __init__.py module16\ |-- my_script.py __init__.py :# This will lift up from .module16 import my_script my_script.py def meaning_of_life(): return 42Usage test,see that don't need module16 name in import: >>> import my_pack >>> my_pack.my_script.meaning_of_life() 42 >>> # Or >>> from my_pack import my_script >>> my_script.meaning_of_life() 42 SpongeB0B Wrote:but none of those work.. can we even go one level up ??So you question for other Thread was not about go one level in a package,but sys.path (how python find folder/files).Maybe this is confusing for you,so my_pack folder in demo then this folder need to be in Python sys.path so Python can find it.SpongeB0B Wrote:I've found a work aroundThis is temporary only for current interpreter session. Look at this post how to add permanently to sys.path .
|