0

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?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • This is silly. There is no way you "have" to give a module a name that is illegal in Python. Use a proper name. – Daniel Roseman Mar 26 '19 at 09:46
  • I know that, but my teacher insists the names should be like that. – Andreiia Praček Mar 26 '19 at 09:47
  • Then find a new teacher **right now**. A teacher who insists on things that are not allowed in Python is worthless. – Daniel Roseman Mar 26 '19 at 09:48
  • Tell your management that their rules go against the syntax of the language they want you to write in. It's clearly a rule made for a different programming language that is being blindly and *stupidly* applied outside its frame of reference. – BoarGules Mar 26 '19 at 09:50
  • possible duplicate: https://stackoverflow.com/questions/8350853/how-to-import-module-when-module-name-has-a-dash-or-hyphen-in-it – Devesh Kumar Singh Mar 26 '19 at 09:51
  • Does this answer your question? [Is it ok to use dashes in Python files when trying to import them?](https://stackoverflow.com/questions/761519/is-it-ok-to-use-dashes-in-python-files-when-trying-to-import-them) – Georgy Aug 12 '20 at 14:39

2 Answers2

0

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

Alec
  • 8,529
  • 8
  • 37
  • 63
0

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
  • I am curious re _"We have to save modules in a specific name"_ though –  Mar 26 '19 at 10:02