0

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?

임지웅
  • 121
  • 1
  • 1
  • 8
  • 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 - https://stackoverflow.com/search?q=python%20circular%20import. –  Nov 21 '18 at 16:13
  • @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! – 임지웅 Nov 21 '18 at 16:24
  • OK. Wish you luck with the research. –  Nov 21 '18 at 16:27
  • Possible duplicate of [How to import other Python files?](https://stackoverflow.com/questions/2349991/how-to-import-other-python-files) – Joooeey Nov 21 '18 at 19:30

1 Answers1

0

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
nerd100
  • 97
  • 1
  • 8