1

I am running this script (parent.py)

import sys
sys.path.append('/python/loanrates/test')
import test2
test2

from this directory:

D:\python\loanrates\Parent

Which opens this script (test.py)

import json 
data = 'ello world'
with open( 'it_worked.json', 'w') as f:
    json.dump(data, f)

From this directiory

D:\python\loanrates\test

When I run parent.py which consequentially runs test.py I want the json.dump to save in D:\python\loanrates\test' currently it saves inD:\python\loanrates\Parent`

EDIT: I have made the following edits:

This is now the child file:

import json 

def main():

    data = 'ello world'

    print data 

    with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
        json.dump(data, f)

if __name__ == '__main__':
    main()

And this is my parent:

import sys
import thread

sys.path.append('/python/loanrates/test')

import test2

thread.start_new_thread(test2.main())

I get this error:

TypeError: start_new_thread expected at least 2 arguments, got 1

What is the second argument i need to put in ?

David Hancock
  • 1,063
  • 4
  • 16
  • 28
  • Don't append to the system path, use the `imp` lib or `__import__` for absolute paths. – Torxed Feb 21 '16 at 09:39
  • Hm i don't know how to use these or why I shouldn't append 'system path' care to elaborate ? @Torxed – David Hancock Feb 21 '16 at 09:43
  • I changed your title, because the "original" folder would be `parent` not test, since you're executing out of `parent` and not test. Also obviously you can append to system path and I guess there's technically not anything wrong with it. But my experience working with custom imports is that it's best to use the import libraries that can handle custom namespaces, global variables uniquely for that library/module. – Torxed Feb 21 '16 at 10:27

2 Answers2

1

../script/test/testing.py

import os
local_path = os.path.dirname(__file__)

with open(local_path + '/output.txt', 'w') as fh:
    fh.write('here i am')

../script/parent/parent.py

import importlib.machinery, imp

namespace = 'testing'
fullPath = r'C:\Users\OpenWindows\Desktop\script\test\testing.py'

loader = importlib.machinery.SourceFileLoader(namespace, fullPath)
testing = loader.load_module(namespace)

# Code should have been executed in testing.py now.

This is an option, seeing as you want relative paths, you could fetch the path from the local __file__ variable since it contains the path to the modules path and not the execution path.

There's also the option to use import() that can pass global variables where you could tinker with changing global variables to match your needs.

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • Hi Torxed, i'm trying to use my method. Would you mind having a loom at my EDIT ? – David Hancock Feb 21 '16 at 11:05
  • @DavidHancock I've had a look at it, and it's not related to the original question. This is regarding starting threads...? Mind explaining why you don't open a new question or google how to start threads in Python? And what do you need th thread for even? – Torxed Feb 21 '16 at 11:11
  • you're right it's a different question now, i have opened a new question http://stackoverflow.com/questions/35535401/simple-way-to-run-multiple-python-threads thanks – David Hancock Feb 21 '16 at 11:18
  • @DavidHancock Glad to hear. Don't forget to mark any question (yours, mine or any other) as an accepted answer if it solves your problem. (We try to avoid having unanswered questions on SO) – Torxed Feb 21 '16 at 14:42
0

I figured it out:

I changed the code in test.py to

import time 
import json 

data = 'ello world'

with open( 'D:/python/loanrates/test/it_worked.json', 'w') as f:
    json.dump(data, f)
David Hancock
  • 1,063
  • 4
  • 16
  • 28
  • I would say that this isn't a long term solution. It's a temporary fix. Adding absolute paths would _always_ work and is a given *first try* solution. Your question was regarding relative paths with imported libraries not saving in it's local residing folder? – Torxed Feb 21 '16 at 10:28
  • This is also extremely platform dependant. – Torxed Feb 21 '16 at 10:47