Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing Program Wide
#1
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
Reply
#2
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?
Reply
#3
(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
Reply
#4
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.
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Some help with my first python class and importing ....im making a lidar program jttolleson 7 1,144 Jul-28-2023, 08:34 AM
Last Post: jttolleson
  How to transform from wide to long format in python shantanu97 1 1,614 Nov-21-2021, 11:53 AM
Last Post: buran
  What is the best way to set application-wide config values? ajorona 1 1,856 May-07-2020, 05:03 PM
Last Post: buran
  output a list of random numbers 'x' columns wide adityavpratap 4 2,923 Jan-13-2020, 05:32 PM
Last Post: perfringo
  problem with importing my program meems 1 3,759 Nov-27-2016, 01:19 AM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020