Python Forum

Full Version: With pyCharm how to run(build) the whole(3 files)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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".
Not sure how this applies to PyCharm.  If you are looking to make an executable file, you might look at py2exe (windows) or freeze (linux) or pytoapp (mac) or cx_freeze (windows and linux).  I've not used any of them, so cannot make a recommendation.
If you're talking about the python interpreter, it compiles each file as it is
imported. This means that all of the files have to be saved in order to get
the latest versions. Otherwise, it's possible to make a change to one of the
peripheral modules, not save it, and import an old version (only if the new
one has not been saved yet).

If you think about it, this makes sense, since import which is part of python,
gets it's modules from the local repository and knows nothing about PyCharm.

To assure that this is done properly, click 'save all' before running your target
script. You will then be sure to get the latest version. I do this as habit.
(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.
A package is a collection of modules(.py files) in a directory/directories .
These are first steps.

So are there many way to distribute your code,
PyPi, Wheel, Freeze code(Pyinstaller,CxFreeze...ect), GitHub, BitBucket.
After many trials all things are solved. All files must have names terminated by ".py". The fist is named main.py. At the bottom (not the top) of the main I put import second_file.py. At the bottom of the second_file I put import third_file.py. So, at the output I have the results of my all 3 files, one after the other.
That works, but it's not the proper way to do it. Also it's obvious that the code in your files is not organised in functions (that would allow to easy reuse your code by import in other scripts).
Let's say you have 3 files. If you want to use code from let's say file2 and file3 in file1 you import them. The import is done at the top of the file, as prescribed in PEP8

file1.py (the main one)
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 this

file1.py (the main one) - alternative
import 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.


Note the use of if __name__ == '__main__':
This way if you execute the file2 as a script it will run the foo2 function. It also allows to import the file2 in file1, without executing foo2 at the time of import, but only when you call it.

Further reading from the docs:
https://docs.python.org/2/tutorial/modules.html
Thank you Buran for your cooperation. I see you are a professional. I think all those things, you don't find easily in a book
(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 book
Depends 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.
To Buran. I think I improved your work:
# 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
I am in file2
I am in file3
This is function foo1 from file1.py

Process finished with exit code 0
Maybe we can improve it more
Actually you did exactly the opposite :-)

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
file3
that you have in the def foo1() and at the end of the file1 don't have any visual effect in your output.

I recommend you to read the doc's link at the bottom of my previous post or any other decent tutorial. you really need to understand the import concept.
Pages: 1 2