Skip to content

Import not working inside def (function) #25

@henriquessb

Description

@henriquessb

I used the debug2production.py script as a base. When I try to use a function from an import inside a function that I created, it says the name of the import (time, in my example) is not defined (line 40). Everything works fine when I don't use functions. Also, the script only runs directly if __name__ == 'pypreprocessor'. If only __name__ =='__main__' is used, it only runs on the output file without preprocessing.

import sys
import time
from pypreprocessor import pypreprocessor

outputFile = 'output_file.py'
start_time = time.time()

#exclude
# run the script in 'debug' mode
if 'debug' in sys.argv:
    pypreprocessor.defines.append('debug')

# run the script in 'production' mode
if 'production' in sys.argv:
    pypreprocessor.defines.append('production')
    pypreprocessor.output = outputFile
    pypreprocessor.removeMeta = True

# run the script in 'postprocessed' mode
if 'postprocessed' in sys.argv:
    pypreprocessor.defines.append('postprocessed')
    pypreprocessor.output = outputFile

pypreprocessor.parse()
#endexclude

def do_stuff():
    global outputFile, start_time
#ifdef debug
    print('This script is running in \'debug\' mode')
#else
#ifdef production
    print('This script is running in \'production\' mode')
    print('To see the output open ' + outputFile)
#else
#ifdef postprocessed
    print('This script is running in \'postprocessed\' mode')
    print('To see the output open ' + outputFile)
#endifall
    print(time.time()-start_time)

if __name__ == '__main__' or __name__ == 'pypreprocessor':
    do_stuff()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions