'How to import a variable from a function in a different directory
I want to import my list
variable which is in my build_video
function and use it in a different file. I've been able to import global variables and use them in different files (in different directories), but have been struggling to do this when the variable I want to import is inside a function.
Here is the build_video
function and my test
variable:
def build_video():
build = []
start = test + "/Video/"
for ttest, apps in test:
for test in test:
test.append("This is a ", test")
I want to store my test
variable inside test
, which is in a different file, in a different directory.
test = [test]
Any suggestions?
Solution 1:[1]
Ohh I think python will not allow you to do so. but if you want to access it better use global variables.
Let us say that your minifest_list
variables are in a directory with the name
Variable_files
and you want to import it in a run method.
you can do it in different ways one way is to import the single variable and then use the variables name like this
from Variables_files.importantvariables import myfavorite_variable
the other way may be to import the whole file and then use the variables by accessing them with filename.variable_name like below
from Variables_files import importantvariables
and then use the variables by accessing them with the dot (.) operator
myvar= importantvariables.manifest_list
Hope i answer your question feel free to ask questions.
Solution 2:[2]
I'm not sure if this is the answer, but maybe you can use the extend() method?
manifest_list = [1,2,3]
type_list = [4,5,6]
manifest_list.extend(type_list)
print(manifest_list)
At the same time, you might also use a global variable because you will have a problem with scope.
Solution 3:[3]
It's impossible. This is local variable available only in this function.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | dchoruzy |