【问题标题】:Python Syntax Error in p++ compilation and setupp++ 编译和设置中的 Python 语法错误
【发布时间】:2015-09-15 15:05:17
【问题描述】:

我正在尝试将 py++ 移植到最新版本的 python,但遇到了它抱怨的以下代码。我是 python 新手。任何帮助都非常感谢,并提前感谢。

错误:

File "C:\tp\python\python34\Lib\site-packages\pyplusplus\gui\wizard.py", line
40
    , 'include_paths' : `self._parser_configuration.include_paths`
                        ^
SyntaxError: invalid syntax

代码如下:

"""generates Py++ code from the user data"""

CODE_TEMPLATE = \
"""
import os
from pyplusplus import module_builder

#Creating an instance of class that will help you to expose your declarations
mb = module_builder.module_builder_t( [r"%(file_path)s"]
                                      , gccxml_path=r"%(gccxml_path)s" 
                                      , working_directory=r"%(working_dir)s"
                                      , include_paths=%(include_paths)s
                                      , define_symbols=%(define_symbols)s )


#Well, don't you want to see what is going on?
mb.print_declarations()

#Creating code creator. After this step you should not modify/customize declarations.
mb.build_code_creator( module_name='pyplusplus' )

#Writing code to file.
mb.write_module( './bindings.cpp' )
"""

class wizard_t( object ):
    """code generator that creates Py++ code"""
    def __init__( self
                  , parser_configuration
                  , source_file ):
        object.__init__( self )
        self._parser_configuration = parser_configuration
        self._source_file = source_file

    def create_code( self ):
        global CODE_TEMPLATE
        return CODE_TEMPLATE % {
            'gccxml_path' : self._parser_configuration.gccxml_path
            , 'working_dir' : self._parser_configuration.working_directory
            , 'include_paths' : `self._parser_configuration.include_paths`
            , 'define_symbols' : `self._parser_configuration.define_symbols`
            , "file_path" : `self._source_file`
        }

【问题讨论】:

  • 你为什么在这些路径周围使用反引号?这些反引号仅在 Python 2 中有效,最好用 repr() 调用替换。
  • 非常感谢。这不是我的代码。它是 py++ 的一部分,它是使用 boost python 的 c++ 到 python 代码生成器。我正在学习 python,因为我正在尝试安装这个工具。我真的不太了解python,无法弄清楚。
  • 显然该代码是为 Python 2 编写的。不过,使用反引号而不是 repr() 非常晦涩(这是语言中删除语法的原因之一)。

标签: python py++


【解决方案1】:

您使用的是 Python 2 语法; Python 3 不支持值周围的反引号:

`self._parser_configuration.include_paths`

您必须在那里使用repr() 函数:

repr(self._parser_configuration.include_paths)

反引号(记录为 string conversion expression 是一种非常晦涩且很少使用的 Python 2 语法;您的代码可能是用 Python 2 编写的。您的代码库中可能还有其他特定于 Python-2 的语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多