1

I want to import file 'a' into file 'b' how to do it ? I tried with os,sys etc but it doesnt work for me. I just want to go 2 folders up and go into file a. I hope that its understable.

file a : C:\Web\Tests\Current\Automated tests\Common\extensions\file.py

file b: C:\Web\Tests\Current\Automated tests\EAW\extensions\targetFile.py

  • You mean, you want to *append* the contents of file A to the contents of file B? And: what did you try so far, please post your code. – jbndlr Oct 18 '16 at 11:09

2 Answers2

0

At top of file b, append file a path into sys.path

For your case, added line below into file_b.py

sys.path.append(r'C:\Web\Tests\Current\Automated tests\Common\extensions')
import file_a
Skycc
  • 3,496
  • 1
  • 12
  • 18
  • ok, but how to do it more generic without hardcoding a path ? – user3162626 Oct 18 '16 at 11:44
  • Thread below might answer your question, you need relative import. But this require you organize your scripts in package. http://stackoverflow.com/questions/7505988/importing-from-a-relative-path-in-python – Skycc Oct 18 '16 at 12:09
  • Or alternatively you could do dir=os.path.dirname( \_\_file\_\_ );sys.path.append(os.path.join(dir, "..\\..", "Common", "extensions")) – Skycc Oct 18 '16 at 12:20
0
import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..',  '..' , 'Common'     , 'extensions'))
import library

this resolved my problem thanks !