r/learnpython • if MacItaly: return helpful / else: return to_Google • Nov 24 '20

Beginner/Intermediate Python Programmer Finally Using Multiple Files for Project

Holy Moley! I started playing around with building a mini-project using multiple files instead of one long file. Wait, let me back up...

I just started Python about a year ago, self-taught, and have been actively building desktop apps for my company in the CMP Semiconductor field. They're efficient applications that my boss loves and have been able to make us some good $$$. But, my codes are over 10,000 lines long. And I knew there had to be a better way.

Fast forward to today where I built a main file, and put a class in another file with some basic math functions. Then in my main file I built a little GUI to solve simple math problems. (I LOVE PYSIMPLEGUI, a big shout out to u/MikeTheWatchGuy for all the work he does.) And I just imported the class into my main and called it as normal...and it works. Easy as pie. I couldn't believe it. I feel like I've just learned a super valuable piece of information that I had been missing out on. Milestone achieved.

Thanks for reading and sharing in my epiphany!

433 Upvotes

75 comments sorted by

View all comments

41

u/shuttup_meg Nov 24 '20

Finally Using Multiple Files for Project

You are stepping up to "modules" and "packages." Check this out:

https://realpython.com/python-modules-packages/

15

u/MacItaly if MacItaly: return helpful / else: return to_Google Nov 24 '20

+1 for you!

Fantastic! This will provide clarity and a concise guide for further progression. Thank you!

4

u/[deleted] Nov 25 '20

Also, learning a language like C# has really helped me learn about encapsulation, inheritance, polymorphism and abstraction. Even though Python technically is "OOP", a language like C# where it's strictly enforced and has different class files, etc has really helped me with structuring and breaking up my files.

1

u/MacItaly if MacItaly: return helpful / else: return to_Google Nov 25 '20

In my free time I've been learning C#. Trying to take my programming to the next level.

2

u/rocket_kielbasa Nov 25 '20

Hey man 10000 lines of code that is AMAZING! I’m also trying to create a data dashboard sort of thing, could you please tell me how/what you used to make the real time graphs? And can I display the graph using the module you mentioned?

1

u/MacItaly if MacItaly: return helpful / else: return to_Google Nov 25 '20

I use matplotlib. I use the interactive mode and collect the data and display it in real-time.

Here is a mini-tutorial of creating an interactive plot in matplotlib. I hope it helps...

https://matplotlib.org/3.3.0/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py

1

u/barbequed-code Nov 25 '20

One more thing, that, for some reason neither that excellent article nor PyPi docs mention, once you put your package's source on git, you'll want to install from that repo:

pip install -e /path/to/local/clone/of/repo

Then, whenever you update the code in your repo, your subsequent imports will automatically use that code.

5

u/Finally_Adult Nov 24 '20

Will pyinstaller (forgive my terminology here, just started playing with python apps yesterday) compile all the modules you create automatically?

5

u/shuttup_meg Nov 24 '20

Yep, pyinstaller, py2exe, etc know how to find all the modules and packages you use and bundle them with your main.py app. Sometimes you have to give them hints in their config/setup, but there are examples.

3

u/[deleted] Nov 25 '20

Keep in mind there was an issue with pyinstaller and python 3.8, unsure if it has been fixed but I have switched back to 3.7 python and have no issues.

1

u/MacItaly if MacItaly: return helpful / else: return to_Google Nov 25 '20

YES! It's with pyinstaller. I went through a number of pyinstaller member forums and found out how they are working around it. If you'd like to know just send me a message and I'll walk you through it.

3

u/MacItaly if MacItaly: return helpful / else: return to_Google Nov 24 '20

pyinstaller -F yourscript.py

The -F tells PyInstaller to compile it into a single .exe file. So all of the additional python files that I import into the main file are pulled into a single executable file.