Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Convert String into Variable Name in Python
Next article icon

Python's behavior is greatly influenced by its environment variables. One of those variables is PYTHONPATH. It is used to set the path for the user-defined modules so that it can be directly imported into a Python program. It is also responsible for handling the default search path for Python Modules. The PYTHONPATH variable holds a string with the name of various directories that need to be added to the sys.path directory list by Python. The primary use of this variable is to allow users to import modules that are not made installable yet. Let's try to understand the concept using an example.

Suppose you defined a module as below:

Python3
# user-defined module# saved as my_module.pydefmodule_func():print("You just imported the user-defined module")

Now if you attempt to import the_my_module.py in your python script as below:

Python3
# python script to# call the module# import the moduleimportmy_module# call the module functionmy_module.module_func()

This will lead to the following error:

This is due to the reason that PYTHONPATH is not set yet. In simpler words, the Python interpreter cannot find the location ofmy_module.py file. So to set PYTHONPATH on awindowsmachine follow the below steps:

Step 1: Open yourThis PC (or My Computer) and write click and click onproperties.

Step 2: After the properties window pop up click on toAdvance System Settings:

Step 3: Now click on the environment variable button in the new popped up window as shown below:

Step 4: Now in the new Environment Variable dialog box click on New as shown below:

Step 5: Now in the variable dialog box add the name of the variable as PYTHONPATH and in value add the location to the module directory that you want python to check every time as shown below:

Step 6: Now open your command prompt and execute the my_script.py file with the below command:

python my_script.py

The behavior of python is set now so it should have no problem executing the python script by importing themy_module.py as shown below:


Improve
Article Tags :
Practice Tags :

Similar Reads

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 ourCookie 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

[8]ページ先頭

©2009-2025 Movatter.jp