1

variables.py

path="c:/something"
path2="c:/somethng2"
path3="c:/something3"
....

main.py

def run():
    from variables import *

...

i have such error:

SyntaxError: import * is not allowed in function 'run' because it contains a nested function with free variables

1
  • import variables than use variables.path, variables.path2,.... Commented May 25, 2017 at 10:22

2 Answers 2

2
import variables as v
path=v.path

This should do for path variable from other file
Hope this helps!

Sign up to request clarification or add additional context in comments.

4 Comments

if i did mypath1=variables.mypath1 unfortunately i still have such error: NameError: global name variables is not defined
may be you are still doing from variables import * the following is working for me import variables path=variables.path print path
Why don't you just do the import at the top itself
thanks. it helped. anyway global variables sucks in python :)
1

You should be very careful with global variables. It is considered a better practice to avoid using global variables, updating them in different locations in the code. It should not be a problem if those are only constants, though. Usually I do it this way:

variables.py:

MY_PATH1 ="c:/something"
MY_PATH2 ="c:/somethng2"
MY_PATH3 ="c:/something3"

main.py

from variables import *

def run():
    print(MY_PATH1)
    ...

If you are working with path strings, you may also want to take a look in the documentation for the os.path module. It helps making path descriptors compatible with the different platforms, among other useful things.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.