2

I understand how to actually link python files, however, i don't understand how to get variable's from these linked files. I've tried to grab them and I keep getting NameError.

How do I go about doing this? The reason i want to link files is to simply neaten up my script and not make it 10000000000 lines long. Also, in the imported python script, do i have to import everything again? Another question, do i use the self function when using another scripts functions?

ie;

Main Script:

import sys, os
import importedpyfile

Imported Py File

import sys, os
GoodPie
  • 967
  • 3
  • 23
  • 41

3 Answers3

3

I understand how to actually link python files, however, i don't understand how to get variable's from these linked files. I've tried to grab them and I keep getting NameError.

How are you doing that? Post more code. For instance, the following works:

file1.py

#!/usr/bin/env python

from file2 import file2_func, file2_variable

file2_func()
print file2_variable

file2.py:

#!/usr/bin/env python

file2_variable = "I'm a variable!"

def file2_func():
    print "Hello World!"

Also, in the imported python script, do i have to import everything again?

Nope, modules should be imported when the python interpreter reads that file.

Another question, do i use the self function when using another scripts functions?

Nope, that's usually to access class members. See python self explained.

There is also more than one way to import files. See the other answer for some explanations.

Community
  • 1
  • 1
Michael Rawson
  • 1,905
  • 1
  • 15
  • 25
  • Thankyou. Such a simple answer and what i was looking for ^_^ – GoodPie Jan 13 '13 at 09:34
  • What do you mean? If you call a function defined in another file, when that function returns or otherwise exits, control will pass back to the calling function in the original file. – Michael Rawson Jan 13 '13 at 10:39
2

I think what you are trying to ask is how to get access to global vars from on .py file without having to deal with namespaces.

In your main script, replace the call to import importedpyfile to say this instead

from importedpyfile import *

Ideally, you keep the code the way you have it. But instead, just reference those global vars with the importedpyfile namespace.

e.g.

import importedpyfile
importedpyfile.MyFunction()  # calls "MyFunction" that is defined in importedpyfile.py
selbie
  • 100,020
  • 15
  • 103
  • 173
  • @IgnacioVazquez-Abrams - Care to explain? You may not "like" the answer, but it will likely satisfy the OP's question. I was about to append a paragraph about accessing vars from namespaces, but haters gonna hate. – selbie Jan 13 '13 at 09:25
  • For one thing, wildcard imports overwrite. Thus, if you import from `foo.py` and `bar.py` in that order, and they both have global vars names `baz`, then `foo.baz` will be overwritten by `bar.baz` – inspectorG4dget Jan 13 '13 at 09:27
  • Geez... ok, I updated the answer to suggest keeping with namespaces. – selbie Jan 13 '13 at 09:27
  • http://stackoverflow.com/q/14277629/20862 http://stackoverflow.com/q/4495469/20862 – Ignacio Vazquez-Abrams Jan 13 '13 at 09:28
  • Thankyou, and i realised my question was rather stupid and i should have just tried other ways myself and figured it out myself. Sorry for the stupid question. :C – GoodPie Jan 13 '13 at 09:31
1

Python modules are not "linked" in the sense of C/C++ linking libraries into an executable. A Python import operation creates a name that refers to the imported module; without this name there is no (direct) way to access another module.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358