How Do You Include Header Files in Python?

  • Context: Python 
  • Thread starter Thread starter cpscdave
  • Start date Start date
  • Tags Tags
    Block Code Python
Click For Summary

Discussion Overview

The discussion centers around the inclusion of header files in Python, drawing comparisons to C programming practices. Participants explore methods for organizing and importing predefined data and structures in Python, as well as the implications of different import styles.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes their experience with header files in C and seeks a similar method in Python, initially attempting to use "import myHeader".
  • Another participant confirms that the approach found by the original poster, using "from myHeader import *", is indeed a Pythonic way to include data, but notes that Python lacks C's macro features.
  • Some participants suggest using "import myHeader as mh" to avoid namespace pollution and recommend accessing objects with a prefix to maintain clarity.
  • Concerns are raised about using "from myHeader import *", with a participant warning that it can lead to name collisions and subtle bugs in the program.
  • One participant proposes using JSON for data-only imports as an alternative method for handling predefined data.

Areas of Agreement / Disagreement

Participants generally agree on the Pythonic methods for importing modules but express differing opinions on the use of wildcard imports and the potential risks associated with them. The discussion remains unresolved regarding the best practices for importing data.

Contextual Notes

Limitations include the lack of consensus on the best import style and the potential for namespace collisions when using wildcard imports. There are also varying opinions on the appropriateness of using JSON for data management.

Who May Find This Useful

Readers interested in Python programming, particularly those transitioning from C or looking for best practices in module imports and data organization.

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   Reactions: 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:

Similar threads

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