Suppose you have this file named

def func(x):
    y = raw_input(x)
    return y


And then you have this calling code:

z = raw_input('Type a request for input and hit enter: ')
omega = execfile('')

Why does the output always end up being "None?"

The return value of execfile is always None. All it does is to execute the code in the file. It does not check whether the last statement in the file is an expression and if so return its value or anything like that.

If you want to use func's return value, you should remove the call to func from, import func (or from func import func in your calling code and then call func.func(z) (or just func(z) if you used from) in that file.

Since your can't stand on its own anyway (z wouldn't be defined if you tried to run it directly), I assume that removing the call won't be a problem.

So like this:

def func(x):
    y = raw_input(x)
    return y

#then in separate file
import func(func)
a = raw_input('Type an input request: ')
b = func.func(a)

# Do whatever with b

Have I got that right?

It's just import func - import func(func) isn't legal syntax in Python. Other than that: yes, like that.

In my PythonWin, 'import func' gives me the error "ImportError: No module named 'func.'" I have to switch over to Linux to try it there. I'll be back if it doesn't work in Linux, either, and otherwise I'll mark the question solved.

But execfile('') produced no such error?

If both files are in the same directory, I don't know why it wouldn't work. If they aren't exefile shouldn't have worked either. So my only idea is that you maybe accidentally somehow moved the files into different directories when you replaced execfile with import - or you created a new file to test import and that file is in a different directory than

It doesn't work in Linux, either, sepp. I'm totally mystified. execfile is totally present and func totally absent. <scratches head>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.