Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Sometimes, for a program with a lot of data, it is common to place the data in an external file. An example is a script that produces an HTML report, using an external file to hold a template.

In Java, the most recommended way to retrieve a resource of the program is to use getClass().getClassLoader().getResource() or getClass().getClassLoader().getResourceAsStream() for a stream.

The advantage is that this is independent of the file system. Also, it works whether the classes are in the file system or the application is distributed as a Jar file.

How do you achieve the same in Python ? What if you're using py2exe or Freeze to generate a stand-alone running app, as seen in this questionthis question ?

Sometimes, for a program with a lot of data, it is common to place the data in an external file. An example is a script that produces an HTML report, using an external file to hold a template.

In Java, the most recommended way to retrieve a resource of the program is to use getClass().getClassLoader().getResource() or getClass().getClassLoader().getResourceAsStream() for a stream.

The advantage is that this is independent of the file system. Also, it works whether the classes are in the file system or the application is distributed as a Jar file.

How do you achieve the same in Python ? What if you're using py2exe or Freeze to generate a stand-alone running app, as seen in this question ?

Sometimes, for a program with a lot of data, it is common to place the data in an external file. An example is a script that produces an HTML report, using an external file to hold a template.

In Java, the most recommended way to retrieve a resource of the program is to use getClass().getClassLoader().getResource() or getClass().getClassLoader().getResourceAsStream() for a stream.

The advantage is that this is independent of the file system. Also, it works whether the classes are in the file system or the application is distributed as a Jar file.

How do you achieve the same in Python ? What if you're using py2exe or Freeze to generate a stand-alone running app, as seen in this question ?

Source Link
Leonel

How to access templates in Python ?

Sometimes, for a program with a lot of data, it is common to place the data in an external file. An example is a script that produces an HTML report, using an external file to hold a template.

In Java, the most recommended way to retrieve a resource of the program is to use getClass().getClassLoader().getResource() or getClass().getClassLoader().getResourceAsStream() for a stream.

The advantage is that this is independent of the file system. Also, it works whether the classes are in the file system or the application is distributed as a Jar file.

How do you achieve the same in Python ? What if you're using py2exe or Freeze to generate a stand-alone running app, as seen in this question ?

lang-py