简体   繁体   English

在Python中,sys.path.append('path / to / module')引发语法错误

[英]In Python, sys.path.append('path/to/module') throws syntax error

I am trying to append the module path to my PYTHONPATH environment variable something like this 我正在尝试将模块路径附加到我的PYTHONPATH环境变量中,如下所示

import sys
sys.path.append(0,"/path/to/module/abc.py")

I am getting syntax error 我收到语法错误

Syntax error: word unexpected (expecting ")")

Can anyone help me with correct syntax for sys.path.append() ? 任何人都可以通过sys.path.append()正确语法来帮助我吗?

Both answers are correct. 两个答案都是正确的。

append() by default adds your argument to the end of the list. 默认情况下, append()将您的参数添加到列表的末尾 It's throwing a syntax error as you are passing it 2 arguments and it is only accepting 1. 向其传递2个参数并且仅接受1时,它将引发语法错误。

Judging by your syntax you want your path added to the front of the path so insert() is the method to use. 根据语法判断,您希望将路径添加到路径的前面 ,因此使用insert()方法。

You can read more in the documentation on Data Structures 您可以在有关数据结构的文档中阅读更多内容

list.append(x)

Add an item to the end of the list; 在列表末尾添加一个项目; equivalent to a[len(a):] = [x]. 等效于a [len(a):] = [x]。

list.insert(i, x)

Insert an item at a given position. 在给定位置插入项目。 The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x) . 第一个参数是要插入元素的索引,因此a.insert(0, x)插入列表的最前面,而a.insert(len(a), x)等效于a.append(x)

import sys
# Inserts at the front of your path
sys.path.insert(0, "/path/to/module/abc.py")
# Inserts at the end of your path
sys.path.append('/path/to/module/abc.py')

Why do you use import sys sys.path.append(0,"/path/to/module/abc.py"); 为什么要使用import sys sys.path.append(0,“ / path / to / module / abc.py”);

Just try: 你试一试:

import sys

sys.path.append('/path/to/module/abc.py')

You could insert it rather than append if you prefer: 如果愿意,可以插入而不是附加:

import sys

sys.path.insert(0, "/home/btilley/brads_py_modules")

import your_modules

Here I have shown an example for help Appending module to path. 在这里,我展示了一个帮助模块添加到路径的示例。 paths is a list that has location of directories stored. 路径是一个列表,其中存储了目录的位置。

def _get_modules(self, paths, toplevel=True):
    """Take files from the command line even if they don't end with .py."""
    modules = []
    for path in paths:
        path = os.path.abspath(path)
        if toplevel and path.endswith('.pyc'):
            sys.exit('.pyc files are not supported: {0}'.format(path))
        if os.path.isfile(path) and (path.endswith('.py') or toplevel):
            modules.append(path)                        
        elif os.path.isdir(path):                       
            subpaths = [
                os.path.join(path, filename)
                for filename in sorted(os.listdir(path))]           
            modules.extend(self._get_modules(subpaths, toplevel=False))
        elif toplevel:
            sys.exit('Error: %s could not be found.' % path)
    return modules

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM