I cant figure out how to add my simple function to my main program file. why not ?
when i do this:
import print_text
echothis("this is text")
exit()
cant understand why people think this is such a bad question.
this doesnt work either:
print_text.echothis("this is text")
same thing happens if i type any of the answers below.
including:
from print_text import echothis
I just get this error:
from: can't read /var/mail/print_text
./blah3.py: line 3: syntax error near unexpected token `"this is text"'
./blah3.py: line 3: `print_text.echothis("this is text")'
or a variant without the /var/mail line…
*this file is named print_text.py*
#!/usr/bin/env python
import time
import random
import string
import threading
import sys
def echothis(txt):
woo=txt
stdout.write(woo)
EDIT: You’re actually not having a python issue but a bash one. You’re running your python script as if it were bash (hence the ‘from: can’t read from’), did you put
#!/usr/bin/env pythonat the beginning of the file you’re running (notprint_text.py, the other one)? You could alternatively call it that way:python myfile.pyand it should work.When you import a module, it is namespaced, so if you want to use anything that is from that module, you need to call it using the proper namespace. Here, you would call you
echothisfunction usingprint_text.echothis.Alternatively, if you want to include
echothisin your main namespace, you can use thefrom print_text import echothissyntax.