Python Forum
using import with filename starting by a number followed by underscore
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using import with filename starting by a number followed by underscore
#1
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 ?
[Image: NfRQr9R.jpg]
Reply
#2
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 563 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  Python 2.7 Import error. Directory and filename conflict petcoo00 2 2,335 Feb-02-2020, 08:46 PM
Last Post: snippsat
  Code syntax with underscore and assigning to a tuple santoshbwn 1 1,934 Jun-24-2019, 12:05 PM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020