GVKun编程网logo

Python相对导入导致SyntaxError异常(python相对导入问题)

30

对于Python相对导入导致SyntaxError异常感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python相对导入问题,并为您提供关于eclipse的命令行执行python回车出现Syn

对于Python相对导入导致SyntaxError异常感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python相对导入问题,并为您提供关于eclipse 的命令行执行 python 回车出现 SyntaxError: invalid syntax 怎么办、eval SyntaxError:python中的语法无效、linux上运行python脚本,SyntaxError: invalid syntax、Linux:无错误 Python 脚本的 SyntaxError的有用信息。

本文目录一览:

Python相对导入导致SyntaxError异常(python相对导入问题)

Python相对导入导致SyntaxError异常(python相对导入问题)

根据python文档,自python 2.5开始支持相对导入和内部包引用。我当前正在运行Python
2.7.3。因此,我尝试在自己的程序包中实现此功能,以便将其用于更简单的导入。我很惊讶地发现它引发了SyntaxError异常,我希望有人可以帮助您找到原因。

我设置了一个测试目录进行测试:

tester├── __init__.py├── first_level.py└── sub    ├── __init__.py    └── second_level.py

两个__init__.py模块都为空。其他模块是:


# first_level.pyprint "This is the first level of the package"

# sub/second_level.pyimport ..first_levelprint "This is the second level"

当我尝试导入second_level模块时,出现以下错误:

Python 2.7.3 (default, Aug  1 2012, 14:42:42) [GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.57))] on darwinType "help", "copyright", "credits" or "license" for more information.Welcome!>>> import tester>>> import tester.sub.second_levelTraceback (most recent call last):  File "<stdin>", line 1, in <module>  File "tester/sub/second_level.py", line 1    import ..first_level           ^SyntaxError: invalid syntax

我希望这两行可以一个接一个地打印,但是却引发了异常。那么,我导入错误吗?你还有其它的想法吗。

答案1

小编典典

您不能导入类似的模块。 import ..blah是无效的导入语法。你需要做from .. import first_level

eclipse 的命令行执行 python 回车出现 SyntaxError: invalid syntax 怎么办

eclipse 的命令行执行 python 回车出现 SyntaxError: invalid syntax 怎么办

eval SyntaxError:python中的语法无效

eval SyntaxError:python中的语法无效

我想分配:

x0='123'    
x1='123'    
x2='123'    
x3='123'    
x4='123'    
x5='123'    
x6='123'    
x7='123'    
x8='123'    
x9='123'

我编写代码来表示123输入x1或输入时可以得到字符串的输出x8

for i in range(0,10):
    eval("x"+str(i)+"='123'")

Traceback (most recent call last):
File "<stdin>",line 2,in <module>
File "<string>",line 1
  x0='123'
  ^
SyntaxError: invalid syntax

我该怎么做?

linux上运行python脚本,SyntaxError: invalid syntax

linux上运行python脚本,SyntaxError: invalid syntax

1

#!/usr/bin/env python3 开头的这句务必要有,不然就会报错,从line2一直到line6都有报错;

版本是python3,则print必须要有(),不然会报错business :SyntaxError: invalid syntax

#!/usr/bin/env python3
  • import sys
  • import os
  • business = sys.argv[1];
  • pa = sys.argv[2][3:];
  • print (business);
  • print (pa);
  •  

    2 当第一句是“#!/usr/bin/env python”时,print是否带()均能正确输出
     

    更多企业内的技术应用和使用技巧,请移步至我的公众号【程序员实用技能】

    图片

    Linux:无错误 Python 脚本的 SyntaxError

    Linux:无错误 Python 脚本的 SyntaxError

    如何解决Linux:无错误 Python 脚本的 SyntaxError?

    我正在尝试在远程服务器上的 Windows 机器上运行一个可以正常工作的 python 文件:

    import collections
    import pandas as pd
    from pathlib import Path
    import shelve
    import cloudpickle
    import numpy as np
    import typing
    from typing import List
    
    import ray
    from ray.rllib.agents import ppo
    from ray.rllib.utils.spaces.space_utils import flatten_to_single_ndarray
    from ray.tune import register_env
    from ray.rllib.env.base_env import _DUMMY_AGENT_ID
    from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
    
    .
    .
    .
    
    if __name__ == ''__main__'':
        WIN = False
        ray.init(dashboard_port=8263)
        daterange = pd.date_range(''2017-01-01'',periods=35040,freq=''15T'')
        norming_factor = 10  
        actions_module = ActionsModuleContinuous()
        batch_n_days = 1
        kappa = 1000
        seed = 1234
        steps_per_episode = batch_n_days * 24 * 4
        num_episodes = 5
        
        device_config: List = list()
        device_config.append(Device1(WIN=WIN,norming_factor=norming_factor,n_data_points=steps_per_episode))
        device_config.append(Device2(WIN=WIN))
        device_config.append(Device3(WIN=WIN))
        
    

    它返回以下 SyntaxError,没有明显的原因:

    (venv) [<username>@<server> examples]$ python test.py
      File "test.py",line 183
        device_config: List = list()
                     ^
    SyntaxError: invalid Syntax
    

    我尝试删除 : List,这只是将相同的错误移到脚本更远的一个看似任意的位置。非常感谢任何帮助。

    解决方法

    device_config: List = list()

    该语法为变量 device_config 添加类型注释。

    如果出现 SyntaxError: invalid syntax 异常,则表示用于运行代码的 Python 解释器不够新。与您的代码无关,这只是意味着当您在 shell 中输入 python 时,被执行的解释器还不够新。

    我们今天的关于Python相对导入导致SyntaxError异常python相对导入问题的分享就到这里,谢谢您的阅读,如果想了解更多关于eclipse 的命令行执行 python 回车出现 SyntaxError: invalid syntax 怎么办、eval SyntaxError:python中的语法无效、linux上运行python脚本,SyntaxError: invalid syntax、Linux:无错误 Python 脚本的 SyntaxError的相关信息,可以在本站进行搜索。

    本文标签: