Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Environment Variables in Python
Next article icon

Environment Variables in Python

Last Updated : 14 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Environment variables are key-value pairs that live in our system's environment. Python reads some of these variables at startup to determine how to behave, for example, where to look for modules or whether to enter interactive mode.

If there’s ever a conflict between an environment variable and a command-line argument, the command-line argument is given priority. Let’s explore some of the most frequently used environment variables in Python:

PYTHONPATH

Adds user-defined directories to Python’s module search path (sys.path).

Use Case: Helps Python find modules that aren't installed in standard locations.

Example:

export PYTHONPATH="/home/user/mymodules"

PYTHONHOME

Sets the default location for Python’s standard libraries.

Use Case: Useful when embedding Python or using a custom Python distribution.

Example:

prefix/lib/pythonX.X
exec_prefix/lib/pythonX.X

PYTHONSTARTUP

Whenever the python interpreted is first initialized Python looks for a readable file with the name .pythonrc.py in Unix and executes the commands inside it. The path to the same file is stored in the PYTHONSTARTUP variable. These files are responsible for setting up the PYTHONPATH.

PYTHONINSPECT

Forces Python to enter interactive mode after running a script.  It can also make changes to the Python code and force it to enter the inspect mode on program termination. It is equivalent to using the -i command-line option.

Use Case: Helpful during debugging or quick testing.

PYTHONCASEOK (Windows only)

This environment variable is used to ignore all import statements while calling the Python interpreter. It is used to find the first case-insensitive match in an import statement.

Use Case: Useful when working with files on case-sensitive and case-insensitive platforms.

PYTHONVERBOSE

If this variable is set to an empty string, it prints a message every time a module is initialized showing the location of the file or the module from where it has been loaded. It also generates information on module cleanup at the termination of the python program.

Use Case: Helpful for debugging import-related issues.

The above variables are the primary environment variables that are frequently used.

Ways to Run Python Code

There are 3 standard ways of using Python, namely:

  1. Using Interactive Mode
  2. Using Command-line
  3. Using an Integrated Development Environment (IDE)

Let's explore them in detail:

Using Interactive mode:

Python's interactive mode allows us to run code line-by-line directly in the interpreter.

Use these commands to enter interactive mode:

$ python # Unix/Linux

C:\> python # Windows

Example:

Here we will enter the interactive mode and ask Python to solve a simple computation. Look at the image below:

Using command-line:

In this method, we need to create a python file first (ignore if file is already available) and then call interpreter to run it.

Example:

Let's make a python file that simply computes the sum of 5 and 10 and returns the result and save it as gfg.py file. This would look somewhat like the below:

Now execute the file by using the below command:

python gfg.py

This will result in the following:

Using an IDE

IDEs like VSCode, PyCharm, Sublime Text or Jupyter Notebook provide a rich environment for writing, testing and debugging Python code.

Example using Jupyter Notebook:

Here we will write a simple python code and ask the IDE to execute it in Python.

Now if wehit the Run Button on the IDE it will call for the interpreter automatically and execute the program. Below is the output:


Next Article
Environment Variables in Python

R

RajuKumar19
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

    Access environment variable values in Python
    An environment variable is a variable that is created by the Operating System. Environment variables are created in the form of Key-Value pairs. To Access environment variables in Python's we can use the OS module which provides a property called environ that contains environment variables in key-va
    3 min read
    Create virtual environment in Python
    A Python virtual environment is like a personal workspace for your project. It lets you create a separate space where you can install and manage packages without affecting other Python projects on your system. This is especially useful when you're working on multiple projects with different dependen
    4 min read
    Viewing all defined variables in Python
    In this article, we are going to discuss how to view all defined variables in Python. Viewing all defined variables plays a major role while debugging the code. Method 1: Using dir() function dir() is a built-in function to store all the variables inside a program along with the built-in variable fu
    5 min read
    Python Variables
    In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
    6 min read
    Managing Virtual environments in Python Poetry
    Poetry helps you declare, manage, and install dependencies of Python projects, ensuring you have the right stack everywhere. Poetry is a tool for dependency management and packaging in the PHP programming language that helps in managing project dependencies and creating virtual environments. Unlike
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences