Python Forum

Full Version: using import with filename starting by a number followed by underscore
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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_module
but this is not working as python see it as a number with an ending thousand separator Doh

so I use instead
importlib.import_module('16_module')
The import seem working but I cannot call it later on
print(16_module.myfunction())
Output:
invalid decimal literal
same problem here the thoushand separator..

I tried
importlib.import_module('16_module') as test
but this is not accepting the AS ....

any ideas ?
(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.
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 42
Usage 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 around

sys.path.append('the path where is located the .py file that I want to include..')
not totally what I was looking for. But this will do the trick.
This is temporary only for current interpreter session.
Look at this post how to add permanently to sys.path.