How can I add a default path to search for python script files?

I've always had a little problem with how to properly configure Python on Windows.

I already set path =% path%; C: \ python27, so I can open .py files with python. It’s just hard for me to figure out how to change the save directory.

For example, I save all my own scripts in the Documents / Python directory. This is Win7, so no documents of mine. I would like to be able to enter "HelloWorld.py" in IDLE and search in this folder for any suitable script names. I couldn't figure out how to add this directory to the default Python search path, though.

Any ideas?

Here is one try.

>>> import sys >>> sys.path ['C:\\Python27\\Lib\\idlelib', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] >>> sys.path.append('C:\Users\Jimmy\Documents\Python') >>> HelloWorld.py Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> HelloWorld.py NameError: name 'HelloWorld' is not defined ` 
+8
python directory path
source share
4 answers
 import sys sys.path.append(YOUR_PATH) # or .insert(0, YOUR_PATH) may give higher priority 

or set the environment variable $PYTHONPATH

+4
source share

This is not how the script works. Change the %PATH% environment variable containing the directory containing the script, and then run the script from the command line, not IDLE.

+1
source share

Please follow the guide

 sys.path.append(r'C:\Users\Jimmy\Documents\Python') 

You cannot accidentally put \ in a string.

When you look at the error message, note that all path elements provided by the system have \\ to avoid the value \ .

The tutorial explains how to use the r" strings for this easily.

+1
source share

I put this in a comment, but I will reply in response to be a little more thorough. It is not clear if you want to run HelloWorld.py as a script, or if you want to import something inside it. However, these are two different things.

If you just want to run HelloWorld.py from cmd or Powershell, you will need to change the PATH environment variable. On Windows, you do this in My Computer> Properties> Advanced> Environment Variables. Press PATH and add the path to the folder containing HelloWorld.py and save the changes. You will need to restart cmd or Powershell to see the changes and the changes will be saved. (This is a constant change in other words)

If you want to import the contents of HelloWorld, you have several options, but the simplest would be to turn the code you want to import into the HelloWorld.py function. So say your current HelloWorld.py looks like this:

 print "Hello World!" 

Change it like this:

 def hello_world(): print "Hello World!" 

Then you just need to add the path to the folder containing HelloWorld.py in sys.path. Looks like you already did it. So, you can import like this:

 import HelloWorld HelloWorld.hello_world() # Will output: "Hello World!" 

If you still want HelloWorld.py to act like a script, you need to add this to the bottom of your script:

 if __name__ == 'main': hello_world() 

This tells Python to import the file without starting it if it is imported. If it is not imported, it will execute the code in the if block.

Hope this clears it. This is definitely a common source of confusion for people starting out with Python.

+1
source share

All Articles