Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

include a file in a python program

559 views
Skip to first unread message

bussiere bussiere

unread,
Sep 5, 2010, 6:57:30 PM9/5/10
to pytho...@python.org
i've got a python.txt that contain python and it must stay as it (python.txt)

how can i include it in my program ?
import python.txt doesn't work
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 ?
i'am using python3
Regards
Bussiere
fan of torchwood
Google Fan boy

Steven D'Aprano

unread,
Sep 5, 2010, 7:12:19 PM9/5/10
to
On Mon, 06 Sep 2010 00:57:30 +0200, bussiere bussiere wrote:

> i've got a python.txt that contain python and it must stay as it
> (python.txt)

Why? Is it against the law to change it? *wink*



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


You could write a custom importer to handle it, but I can't help you with
that. Try Google.

> 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 ?

fp = open("python.txt")
text = fp.read()
fp.close()
exec(text)


But keep in mind that the contents of python.txt will be executed as if
you had typed it yourself. If you don't trust the source with your life
(or at least with the contents of your computer), don't execute it.

--
Steven

Roy Smith

unread,
Sep 5, 2010, 7:14:52 PM9/5/10
to
In article <mailman.476.12837274...@python.org>,
bussiere bussiere <buss...@gmail.com> wrote:

> i've got a python.txt that contain python and it must stay as it (python.txt)
>
> how can i include it in my program ?
> import python.txt doesn't work
> 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 ?
> i'am using python3

The simple solution (at least for unix-ish systems) would be to make a
symlink python.py -> python.txt. If you don't have the ability to do
that externally, you could even have your python program create the
symlink on the fly, import the module, then delete the symlink. The
symlink could even be in /tmp.

Another possible solution is to read the entire file into a string and
then eval() the string. I'm assuming eval() still exists in python 3;
does it?

Yet another possibility (never tried this, but it seems reasonable)
would be to compile() your text file
(http://docs.python.org/py3k/library/functions.html#compile).

Of course, it would be a lot easier if you just renamed the file, but
I'll take it as a given that there are external forces which prevent you
from doing that.

Roy Smith

unread,
Sep 5, 2010, 7:17:09 PM9/5/10
to
In article <4c8423d3$0$28657$c3e...@news.astraweb.com>,

Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au> wrote:

> fp = open("python.txt")
> text = fp.read()
> fp.close()
> exec(text)

> But keep in mind that the contents of python.txt will be executed as if
> you had typed it yourself. If you don't trust the source with your life
> (or at least with the contents of your computer), don't execute it.

Well, this is true, but eval() or exec() isn't really exposing him to
anything that import isn't.

MRAB

unread,
Sep 5, 2010, 7:27:13 PM9/5/10
to Python List
On 05/09/2010 23:57, bussiere bussiere wrote:
> i've got a python.txt that contain python and it must stay as it (python.txt)
>
> how can i include it in my program ?
> import python.txt doesn't work
> 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 ?
> i'am using python3

import imp
python = imp.load_source("python", "python.txt")

> Regards
> Bussiere
> fan of torchwood
> Google Fan boy

A fan of Torchwood and Google, but not Python? :-)

Terry Reedy

unread,
Sep 5, 2010, 7:39:31 PM9/5/10
to pytho...@python.org
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 xxx.py, 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

Terry Reedy

unread,
Sep 5, 2010, 7:41:39 PM9/5/10
to pytho...@python.org
On 9/5/2010 7:12 PM, Steven D'Aprano wrote:

> fp = open("python.txt")
> text = fp.read()
> fp.close()
> exec(text)
>
> But keep in mind that the contents of python.txt will be executed as if
> you had typed it yourself. If you don't trust the source with your life
> (or at least with the contents of your computer), don't execute it.

Also, this executes the code within the namespace of the calling code
rather than within a fresh module namespace. That could be either better
or worse.

--
Terry Jan Reedy

Steven D'Aprano

unread,
Sep 5, 2010, 7:45:25 PM9/5/10
to
On Mon, 06 Sep 2010 00:27:13 +0100, MRAB wrote:

> import imp
> python = imp.load_source("python", "python.txt")

Nice!


--
Steven

Niklasro(.appspot)

unread,
Sep 6, 2010, 3:50:29 AM9/6/10
to

You can do it with tkinter to also enable GUI.

bussiere maillist

unread,
Sep 7, 2010, 7:37:57 PM9/7/10
to pytho...@python.org
Thanks all for your response i will try out this week, you have give
me sufficient hint.
Thanks again.
Bussiere

> --
> http://mail.python.org/mailman/listinfo/python-list
>

bussiere bussiere

unread,
Sep 7, 2010, 7:47:47 PM9/7/10
to pytho...@python.org
Thanks all for your response i will try out this week, you have give
me sufficient hints.
Thanks again.
Bussiere

Bruno Desthuilliers

unread,
Sep 10, 2010, 10:50:58 AM9/10/10
to
Steven D'Aprano a écrit :

> On Mon, 06 Sep 2010 00:57:30 +0200, bussiere bussiere wrote:
>
>> i've got a python.txt that contain python and it must stay as it
>> (python.txt)
>
> Why? Is it against the law to change it? *wink*
>
>> how can i include it in my program ?
>> import python.txt doesn't work
>
>
> You could write a custom importer to handle it, but I can't help you with
> that. Try Google.
>
>> 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 ?
>
> fp = open("python.txt")
> text = fp.read()
> fp.close()
> exec(text)

or just:

execfile("python.txt")


> But keep in mind that the contents of python.txt will be executed as if
> you had typed it yourself. If you don't trust the source with your life
> (or at least with the contents of your computer), don't execute it.

+10

0 new messages