Page 1 of 1

Calling variables from other directories

Posted: Thu Mar 24, 2016 11:10 am
by complete_NOOB_91
Hi guys,
I am having some trouble with calling variables from other directories, the code that I have produced is quite modular so that it is easy to read and follow. But now after testing that each piece of code works separately I now need to make these different codes operate together. Can someone please advise as I am fairly new to the Raspberry pi, also i am programming in python. Thanks

Re: Calling variables from other directories

Posted: Thu Mar 24, 2016 12:27 pm
by scotty101
To use code in a modular way in python you would import another module.

Code: Select all

import my_other_module
If is in another folder relative to top level function then you might use

Code: Select all

import folder_name.module_name

Code: Select all

from folder_name.module_name import class_or_function
Have a look at these pages
https://docs.python.org/3/reference/import.html
https://docs.python.org/3/tutorial/modules.html

Re: Calling variables from other directories

Posted: Thu Mar 24, 2016 1:30 pm
by mysen

Code: Select all

from file_name import function_name
I think that's what you're looking for, right?

so...

Code: Select all

from functions import addition

answer = addition(a,b)
a and b are sent over to the function in another file, added together and sent back. simples!