
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to import Python modules by default at the time of program starts?
Use the environment variable PYTHONSTARTUP. From the official documentation:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session.
So, just create a python script with the import statements and point the environment variable to it. For instructions on how to create environment variables on your OS see: https://www.java.com/en/download/help/path.xml. Use the variable name as PYTHONSTARTUP instead of PATH.
Having said that, remember that 'Explicit is always better than implicit', so don't rely on this behavior for production scripts. Also the more imports you add slower your python startup time will become.
- Related Articles
- How to install and import Python modules at runtime?
- By default, How Many Modules Does Python Come With?
- How to use multiple modules with Python import Statement?
- How we can import Python modules in Jython?
- How we can import Python modules without installing?
- Python import modules from Zip archives (zipimport)
- How can I import modules for a Python Azure Function?
- Can we iteratively import python modules inside a for loop?
- How to create a YouTube link that starts at a specific time?
- How to check version of python modules?
- How to create YouTube video link that starts at specific time using YouTube Mobile App?
- How to import other Python files?
- How to import Matplotlib in Python?
- How to use remote python modules?
- Program to find nearest time by reusing same digits of given time in python
