Python How Do You Include Header Files in Python?

Click For Summary
In Python, to replicate the functionality of C header files for including predefined data, creating a separate Python file (e.g., "myHeader.py") is a common approach. Importing this file using "import myHeader" does not automatically bring the defined variables into the main namespace. Instead, using "from myHeader import *" can achieve this but is discouraged due to potential namespace collisions and the risk of subtle bugs. A safer alternative is to use "import myHeader as mh," allowing access to the variables with the prefix (e.g., mh.foo). Additionally, for data-only needs, using JSON is recommended, as it provides a structured way to manage data without the risks associated with wildcard imports. To further control what gets imported, defining an "__all__" list in "myHeader.py" can limit the exported symbols.
cpscdave
Messages
402
Reaction score
120
In c I quite often build header files with a bunch of predefined data. Fill it with structures or what not.
Then to include that information in my program its a simple #include "myHeader.h"

Is there a way I can do this in python?
I've created another file "myHeader.py"
Have put in some code to setup these values
then in main file I put import myHeader but it doesn't appear to work as I intend.

Any suggestions??*edit* I figured I can do it by doing
from myHeader import *

SOLVED :) unless there is a more proper way to accomplish this in python :)
 
Technology news on Phys.org
Yes you found the python way of doing it. Python doesn't have any macro features like C and hence no need to pull in a file that way like C.
 
  • Like
Likes cpscdave
Better would be to

import myHeader as mh

then refer to the objects created in myHeader as mh.foo, mh.baz, etc. Or at least limit the names that gets pulled into your namespace using "*" by adding a line at the end of myHeader.py:

__all__ = ['foo', 'bar', 'baz']
 
Daverz said:
Better would be to

import myHeader as mh
Or even
import myHeader

Using "from myHeader import *" is the moral equivalent of "using namespace std;" in C++.
 
cpscdave said:
In c I quite often build header files with a bunch of predefined data. Fill it with structures or what not.
Then to include that information in my program its a simple #include "myHeader.h"
Is there a way I can do this in python?

Data only? Use JSON.

https://docs.python.org/3.4/library/json.html

edit: Ops, didn't see you were doign import *. it's a really bad idea. It imports every symbol from your module into the global namespace, which means there is a 'danger' of a collision. One day you'll add a new data field and the program will develop an error because you re-used a name. With Python, it might not crash and instead just generate subtly incorrect output. Better to specifically import what you need: 'from <x> import <y>' or put everything into a class, and import that one class instead.
 
Last edited:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
Replies
1
Views
5K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 9 ·
Replies
9
Views
1K
Replies
4
Views
5K
  • · Replies 14 ·
Replies
14
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K