include a file in a python program

Terry Reedy tjreedy at
Mon Sep 6 01:39:31 CEST 2010

On 9/5/2010 6:57 PM, bussiere bussiere wrote:
> i've got a python.txt that contain python and it must stay as it (python.txt)

If you are working for someone who is such an idiot as to impose such a 
condition on you, you have our condolences.

> how can i include it in my program ?
> import python.txt doesn't work

A file run directly as a script can be named anything. But as far as I 
know, imported code must be, either in a directory or zipfile.

> is there a way :
> a) to make an include("python.txt")
> b) tell him to treat .txt as .py file that i can make an import python ?

1. Write a custom importer (hard, I have no idea how).
2. Copy the .txt file to .py, either before running the main program or 
within a custom import function.

Terry Jan Reedy

More information about the Python-list mailing list