Import behaviour

I’m trying to port a game from the 1980’s (GW_Basic).
in IDLE, after ‘import startrader_defines_base’, nothing is defined: variables, classes, etc.
eg., >>> starfile
Traceback (most recent call last):
File “<pyshell#19>”, line 1, in
starfile is not defined

What am I doing wrong?

Hey Rick,

starfile isn’t in the current scope, but startrader_defines_base is. Maybe you meant to try:

import startrader_defines_base

# Or, to import just starfile from the module:
from startrader_defines_base import starfile

# Or, maybe the following to import everything within the module:
from startrader_defines_base import *

You can inspect what’s in a module by running:

import startrader_defines_base

Firstly, names starting and ending with two underscores, like
__starfile__, are reserved for Python’s own use. You should name your
variables differently.

Secondly, after importing a module, to access the module’s contents, you
need to access the module first:

>>> import math
>>> pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pi' is not defined
>>> math.pi

You can import a module with a shorter name:

import startrader_defines_base as base

or you can import the contents directly:

from startrader_defines_base import starfile, spam, eggs, aardvark

With care, you can even tell Python to import “everything”, although
this is not good practice and you should generally be very careful about
doing this:

from startrader_defines_base import *

Thanks for the reminder about dunders, and the ‘’ format.