Apr-06-2017, 04:23 AM
Hi all! I have 3 files in the same project. To join files I use import. Is it correct ?? But the most important question is: how to run the whole project, that is the 3 files ?? I see nowhere "build".
(Apr-06-2017, 04:23 AM)sylas Wrote: [ -> ]Hi all! I have 3 files in the same project. To join files I use import.That's okay.
(Apr-06-2017, 04:23 AM)sylas Wrote: [ -> ]But the most important question is: how to run the whole project, that is the 3 files ?? I see nowhere "build".One file to import it's a module.
import file2 import file3 def foo(): print 'This is function foo from file1.py' if __name__ == '__main__': foo() file2.foo2() file3.foo3()file2.py
def foo2(): print 'This is function foo2 from file2.py' if __name__ == '__main__': foo2()file3.py
def foo3(): print 'This is function foo3 from file3.py' if __name__ == '__main__': foo3()now, if you run file1, it will import file2 and file 3. It will also execute the
foo
, file2.foo2
and file3.foo3
functions when you call them (and in the order you call them).Output:This is function foo from file1.py
This is function foo2 from file2.py
This is function foo3 from file3.py
foo2 and foo3 would be available in the entire file1, so you can also do something like thisimport file2 import file3 def foo(): print 'This is function foo from file1.py' file2.foo2() file3.foo3() if __name__ == '__main__': foo()the output would be the same.
if __name__ == '__main__':
(Apr-07-2017, 01:41 PM)sylas Wrote: [ -> ]Thank you Buran for your cooperation. I see you are a professional. I think all those things, you don't find easily in a bookDepends on the book. One of the very first things described after basic syntax is how and why to use functions. Package management is something I would also consider basic, and a part of all books, since there isn't a decently sized project that would be entirely contained in a single file.
# file1.py import file2 import file3 def foo1(): print('This is function foo1 from file1.py') file2 file3 foo1() file2 file3
#file2.py print("I am in file2")
#file3.py print("I am in file3")Here is the output:................................................................................................................................................................................../usr/bin/python3.5 /home/sylvain/PycharmProjects/py2/file1.py
Output:I am in file2 <---------- this line is result of the import file2 done at the top of file1
I am in file3 <---------- and this line is result of the import file3 done at the top of file1
This is function foo1 from file1.py
file2 file3that you have in the
def foo1()
and at the end of the file1 don't have any visual effect in your output.