Python Forum
Importing Program Wide - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Importing Program Wide (/thread-29507.html)



Importing Program Wide - JarredAwesome - Sep-06-2020

Hey everyone,


I’m new to python for, but I have done a lot of php programming.

One feature I liked in php was include. I know Python has import, but that seems to only have a scope to each file it’s in. Is there anyway I can import modules, that will be active in all the other files I open.

For example,

Main.py
Import a
Import b
print(x)
File a.py
x = ‘hello world’
File b.py
print (x)
Output:
hello world
hello world



RE: Importing Program Wide - ndc85430 - Sep-06-2020

You need to import a module in whichever other module you want to use it. Why would it make any sense to have things automatically imported everywhere? Can you give an actual use case where you think you need that?


RE: Importing Program Wide - JarredAwesome - Sep-06-2020

(Sep-06-2020, 06:38 PM)ndc85430 Wrote: Why would it make any sense to have things automatically imported everywhere?

I can make one module filled with functions and class's that I can use through out the programs without having to importing it into every file


RE: Importing Program Wide - ndc85430 - Sep-07-2020

If you've got one module that's used everywhere, that sounds like bad design - seems like you don't have separation of concerns.

It would be good to see a concrete example, though.


RE: Importing Program Wide - JarredAwesome - Sep-07-2020

(Sep-07-2020, 03:40 AM)ndc85430 Wrote: If you've got one module that's used everywhere, that sounds like bad design - seems like you don't have separation of concerns.

It would be good to see a concrete example, though.

I intend to start error logging on the project I’m working on. Now granted this is my first time having to do that manually (in php, the sever was always set up to do that for me). So I wanted to set up a class, or function that I can send errors to. It would automatically add the date, time and source of the error. It would also email me if anything major happened.

Since errors can happen anywhere, it’d be nice if I could autoload that, so I could just call it when it’s needed.

The program I am working on also happens to rely on input from an external API. Different parts of the program need results from it. I’d like to be able to call the API directly from the files that need it.

Also, smaller things. I always find myself formatting stings, or arrays in a specific way. In php and JavaScript, I simply make a function that does that for me, and call it when I need it. Once again, it would be nice if those things were omnipresent in all of the code.