python的sys.path是个列表,为什么append之后变成None了?

jupyter运行

import sys
print(type(sys.path))
try:
    sys.path=sys.path.append("test")
except Exception as e:
    print(e)
print(sys.path)

<class 'list'>
None

阅读 4.1k
1 个回答
In [1]: import sys

In [2]: sys.path
Out[2]:
['',
 'D:\\Program Files\\Python36\\Scripts\\ipython.exe',
 'd:\\program files\\python36\\python36.zip',
 'd:\\program files\\python36\\DLLs',
 'd:\\program files\\python36\\lib',
 'd:\\program files\\python36',
 'C:\\Users\\Jam\\AppData\\Roaming\\Python\\Python36\\site-packages',
 'd:\\program files\\python36\\lib\\site-packages',
 'd:\\program files\\python36\\lib\\site-packages\\win32',
 'd:\\program files\\python36\\lib\\site-packages\\win32\\lib',
 'd:\\program files\\python36\\lib\\site-packages\\Pythonwin',
 'd:\\program files\\python36\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\Jam\\.ipython']

In [3]: sys.path.append('test')

In [4]: sys.path
Out[4]:
['',
 'D:\\Program Files\\Python36\\Scripts\\ipython.exe',
 'd:\\program files\\python36\\python36.zip',
 'd:\\program files\\python36\\DLLs',
 'd:\\program files\\python36\\lib',
 'd:\\program files\\python36',
 'C:\\Users\\Jam\\AppData\\Roaming\\Python\\Python36\\site-packages',
 'd:\\program files\\python36\\lib\\site-packages',
 'd:\\program files\\python36\\lib\\site-packages\\win32',
 'd:\\program files\\python36\\lib\\site-packages\\win32\\lib',
 'd:\\program files\\python36\\lib\\site-packages\\Pythonwin',
 'd:\\program files\\python36\\lib\\site-packages\\IPython\\extensions',
 'C:\\Users\\Jam\\.ipython',
 'test']

list的append不会又返回值,会直接操作list

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题