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
> 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
> 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.
> 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.
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? :-)
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
> 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
> import imp
> python = imp.load_source("python", "python.txt")
Nice!
--
Steven
You can do it with tkinter to also enable GUI.
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