0

Good evening.

I have scriptone.py in folderone and scripttwo.py in foldertwo.

How do I tell scriptone.py to run scripttwo.py from foldertwo

If both scriptone.py and scripttwo.py are in the same folder I can run scripttwo.py with

import scripttwo

But I would really like to run scripttwo.py from foldertwo

Thankyou.

Simon Jeal
  • 153
  • 2
  • 4
  • 14

3 Answers3

1

Look at the environmental variable PYTHONPATH or sys.path.

iAdjunct
  • 2,739
  • 1
  • 18
  • 27
0

This is how I did it in the past:

import os
os.chdir('../foldertwo')
import sys
sys.path.append(os.getcwd())

import scripttwo

This adds the parent folder to the path variable, which holds all the places that Python looks for module when you use import.

El'endia Starman
  • 2,204
  • 21
  • 35
  • In foldertwo a scripttwo.pyo file is created after running. why is this, what is it for and can it be deleted after running the script? – Simon Jeal Nov 26 '15 at 00:36
  • @SimonJeal: I don't typically see `.pyo` files, so I'm not totally sure why that would be. [This question](http://stackoverflow.com/q/8822335/1473772) should help. – El'endia Starman Nov 26 '15 at 00:38
  • Thanks I just read that. Im going to go with I can delete it, as my scripttwo.py will always change. thanks – Simon Jeal Nov 26 '15 at 00:40
-1

Thanks Adjuct

I used

import sys

secondscript = path/to/second/script
sys.path.append(secondscript)

import scripttwo

Working Solved.

By using sys.path.append() it tells python that there is another place to look for a script. So when you

import scripttwo 

it knows to look in the appended location also. Thank you for the advice

Simon Jeal
  • 153
  • 2
  • 4
  • 14