0

I am beginning to think that I cannot use Python import in this way.

font_list = [
  'HIRANGANA_FONT',
  'GREEK_FONT',
  'EXT_LATIN_FONT',
  'CONTROL_FONT',
  'BOX_FONT',
  'BLOCK_FONT',
  'BASIC_LATIN_FONT',
  'CP437_FONT_ROT90',
  'CP437_FONT_ROT180',
  'CP437_FONT_ROT270',
  'VINCENT_FONT',
  'CP437_FONT',
  'SINCLAIRS_FONT',
  'LCD_FONT',
  'TINY_FONT',
];

from fonts_file import i for i in font_list

SyntaxError: invalid syntax

So I try:

for i in font_list:
  from fonts_file import i

ImportError: cannot import name i

Is it possible? Can I import everything in font_list from one line of code?

I know this below works, but I am trying to keep things left justified

from fonts_file import HIRANGANA_FONT, GREEK_FONT, EXT_LATIN_FONT, CONTROL_FONT, BOX_FONT, BLOCK_FONT, BASIC_LATIN_FONT....
Cohan
  • 4,384
  • 2
  • 22
  • 40
WesZ
  • 129
  • 1
  • 10
  • 1
    Why do you want to use `import` in that way at all? You can always use `getattr(fonts_file, i)` to refer to the font whose name is stored in variable `i`; there's nothing special you need the `from module import name` syntax for. (Then again, I wouldn't want to stuff them into your module namespace at all; what's the value to `CP437_FONT` as a word you can use in your code, vs `import fonts_file as f` and then referring to `f.CP327_FONT`?) – Charles Duffy Jan 08 '20 at 17:03
  • What about something like: `import fonts_file as ff`. The use `ff.GREEK_FONT` when you need to use it? – S3DEV Jan 08 '20 at 17:08
  • I'm willing to try anything that works well. I am a novice and I have some python code that I am working with and trying to make it better. So I'll give both those a shot and see how life is. Thanks. – WesZ Jan 08 '20 at 17:11
  • 1
    @WesZ I strongly recommend learning more Python, and about programming in general, before anything else. – AMC Jan 08 '20 at 18:25
  • Thanks AMC, but this really isn't my professional job and I honestly don't use it to feed my family. A year ago I knew nothing of Python... but like I said, Thanks for the advice, but I assure you that I am learning...unless you think asking questions is not part of the learning process? – WesZ Jan 08 '20 at 19:15

1 Answers1

2

You could do this:

from fonts_file import (HIRANGANA_FONT,
                        GREEK_FONT,
                        EXT_LATIN_FONT,
                        CONTROL_FONT,
                        ...
                        )
D Dhaliwal
  • 552
  • 5
  • 23
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 1
    I'd probably put a newline continuation before the first font name to allow those names to be sorted automatically (as with `!sort` in vim). The OP isn't doing that now, but they *should* -- it'll make for fewer merge conflicts if they're merging multiple branches that each change the list, should that list be maintained in a consistent way. – Charles Duffy Jan 08 '20 at 17:04
  • Well Scott, that was "Slick 'n Quick" there buddy !! I used Charles' suggestion as well with a preceding \ and it's all good to go. No code changes, updates, etc. All just a nice neat package all dressed left. Thanks Guys !! – WesZ Jan 08 '20 at 17:45