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 ?

3
  • Don't append to the system path, use the imp lib or __import__ for absolute paths. Commented Feb 21, 2016 at 9:39
  • Hm i don't know how to use these or why I shouldn't append 'system path' care to elaborate ? @Torxed Commented Feb 21, 2016 at 9: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. Commented Feb 21, 2016 at 10:27

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Torxed, i'm trying to use my method. Would you mind having a loom at my EDIT ?
@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?
you're right it's a different question now, i have opened a new question stackoverflow.com/questions/35535401/… thanks
@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)
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)

2 Comments

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?
This is also extremely platform dependant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.