简体   繁体   中英

Importing function from module with hyphen name

I can't import the functions from module with "-" in its name. We have to save modules in a specific name, so I must not change the name. So how should I import this:

from surname-funkcije import izris_kvadrata, NSVN, NSV1

I know how to import the module, it should be something like:

surname_funkcije = __import__("surname-funkcije")

but how about its functions?

That's an illegal module name. Just change the name of the module.

on python 3.7, given surname-funkcije.py with contents as such:

NSVN = 42
NSV1 = 'I do not know what this is'

def izris_kvadrata(a, b):
    return a+b

you can import and use said module as such:

import importlib

m = importlib.import_module('surname-funkcije')
izris_kvadrata, NSVN, NSV1 = m.izris_kvadrata, m.NSVN, m.NSV1

help(izris_kvadrata)
c = izris_kvadrata(NSVN, 6)
print("NSVN = {}; NSV1 = {}".format(NSVN, NSV1))
print("c = {}".format(c))

which gives me output as such:

Help on function izris_kvadrata in module surname-funkcije:

izris_kvadrata(a, b)

NSVN = 42; NSV1 = I do not know what this is
c = 48

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM