0
votes

can some give me right code to get variables from other file

getting variables or function from other python file

getting v in A.py to B.py 1. /var/www/Project/sub/A.py /var/www/Project/sub/B.py

B.y 

from A import v

2. /var/www/Project/sub/stuff/A.py /var/www/Project/sub/B.py

B.y

from stuff.A import v

3. /var/www/Project/sub/stuff/A.py /var/www/Project/sub/stuff/B.py

B.y

import os, sys

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))

from stuff.B import v
  1. /var/www/Project/sub/A.py /var/www/Project/sub/stuff/B.py

    B.y import os, sys

    sys.path.append(os.path.abspath(os.path.join(os.path.dirname(file), os.pardir)))

    from B import v

is this how system goes? is this code right?

1
If this question is about the problem from the previous question asked by you earlier today then the problem is circular import. Please learn about possible solutions from already answered questions - stackoverflow.com/search?q=python%20circular%20import. - Poolka
@Poolka thanks for your solution(circular import), was just wondering about clean way to access other python file in several situation. im aware of your answer about previous question i asked today:) thanks! - 임지웅
OK. Wish you luck with the research. - Poolka
Possible duplicate of How to import other Python files? - Joooeey

1 Answers

0
votes

if you have two files at the same directory, you can simply import one file to the other:

test1.py:

a = 5
b = 10

test2.py:

import test1

print test1.a
print test1.b

or:

from test1 import a,b
print a
print b