Welcome, guest | Sign In | My Account | Store | Cart

Adds the specified path to the Python system path if it is not already there. Takes into account terminating slashes and case (on Windows).

Returns -1 if the path does not exist, 1 if it was added, and 0 if it was not (because it is already present).

Python, 47 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def AddSysPath(new_path):
	import sys, os

	# standardise
	new_path = os.path.abspath(new_path)

	# MS-Windows does not respect case
	if sys.platform == 'win32':
		new_path = new_path.lower()

	# disallow bad paths
	do = -1
	if os.path.exists(new_path):
		do = 1
		
		# check against all paths currently available
		for x in sys.path:
			x = os.path.abspath(x)
			if sys.platform == 'win32':
				x = x.lower()
			if new_path in (x, x + os.sep):
				do = 0

		# add path if we don't already have it
		if do:
			sys.path.append(new_path)
			pass

	return do

if __name__ == '__main__':
	# test
	import sys

	print 'Before:'
	for x in sys.path:
		print x

	if sys.platform == 'win32':
		print AddSysPath('c:\\Temp')
		print AddSysPath('c:\\temp')
	else:
		print AddSysPath('usr/lib/my_modules')

	print 'After:'
	for x in sys.path:
		print x

Modules must be on the Python system path before they can be imported. But we don't always want a huge permanent path, because that slows things down. This simple function dynamically adds a path.

It has been corrected to meet all shortcomings addressed in the comments.

4 comments

Jürgen Hermann 22 years, 11 months ago  # | flag

Improvements. There are two problems with this code: you should call os.path.abspath and os.path.exists on the argument, and also make a second test with os.sep added (sys.path sometimes contains paths with or without a trailing [back]slash).

Steve Holden 22 years, 10 months ago  # | flag

Windows is not case-sensitive, and accepts forward and backward slashes. Extending the wish to avoid duplication, you might want to standardize the case and the separator you use before committing the change to sys.path in Microsoft environments.

Robin Parmar (author) 22 years, 6 months ago  # | flag

Updated. I have updated the code to reflect the shortcomings addressed in the previous two comments. It has been tested on Linux and Win32. Thanks!

Pierre Johnson 21 years, 11 months ago  # | flag

Dynamic or Static? First off, thanks for posting the code. Now, unto my gripe. The concept of dynamically adding a path suggests adding a path expression on-the-fly, i.e., prompting a user for a path expression.

How does this function dynamically add a path as you suggest? The code suggests a static reference toward a file system folder/sub-directory within the namespace of a given file, either MS Windows or a Unix variant.

The explanation of your function might confuse first-time Python developers. Perhaps the following explanation might improve the situation:

For Python to import a module, the Python interpreter requires a reference to the location of the module within the namespace of your file system, i.e., Python needs to know the sub-directory/folder where a module exists. One method that informs the Python interpreter regarding the whereabouts of a module is to use the sys.path.append('your_path_here') function of the sys module that is part of the Python environment.