complete_NOOB_91
Posts: 2
Joined: Wed Mar 09, 2016 6:34 pm

Calling variables from other directories

Thu Mar 24, 2016 11:10 am

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

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Calling variables from other directories

Thu Mar 24, 2016 12:27 pm

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
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

mysen
Posts: 13
Joined: Sun Feb 21, 2016 11:49 pm

Re: Calling variables from other directories

Thu Mar 24, 2016 1:30 pm

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!

Return to “Python”