我对有关导入语句在IPython中的工作方式感到有些困惑.我通过网络搜索找不到任何东西.
隐式相对导入适用于Python 2,但我不知道是否仍然是IPython for Python 3的情况.
使用点语法的相对导入似乎根本不起作用:
In [6]: ls
dsp/ __init__.py __init__.pyc utils/
In [7]: from .utils import capture
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-e7d50007bdd1> in <module>()
----> 1 from .utils import capture
ValueError: Attempted relative import in non-package
Run Code Online (Sandbox Code Playgroud)
导入使用点语法的模块似乎是不可能的:
In [8]: cd utils
/home/user/workspace/mypkg/mypkg/utils
In [9]: ls
capture/ capture.py capture.pyc cext/ __init__.py __init__.pyc
In [10]: from capture import Capture
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-10-8c31c76d052d> in <module>()
----> 1 from capture import Capture
/home/user/workspace/mypkg/mypkg/utils/capture.py …
Run Code Online (Sandbox Code Playgroud) 首先,我对cmake相对较新.我正在尝试使用cmake构建一个具有单个外部依赖项的项目.我将外部项目的INSTALL_DIR指定为CMAKE_INSTALL_PREFIX,因此它将安装到与父项目相同的位置.但是当我运行make时,它会忽略它并尝试安装到/ usr/local/lib.
这是我的CMakeList.txt:
cmake_minimum_required( VERSION 2.8 )
include( ExternalProject )
project( capture )
add_library( capture SHARED capture.cc )
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
ExternalProject_Add( proj_exceptions
GIT_REPOSITORY /home/user/workspace/exceptions
INSTALL_DIR ${CMAKE_INSTALL_PREFIX}
)
add_library( exceptions SHARED IMPORTED )
set_property( TARGET exceptions
PROPERTY IMPORTED_LOCATION ${CMAKE_INSTALL_PREFIX}/lib/libexceptions.so
)
add_dependencies( exceptions proj_exceptions )
include_directories( ${CMAKE_INSTALL_PREFIX}/include )
target_link_libraries( capture exceptions )
install( TARGETS capture DESTINATION lib )
install( FILES capture.h DESTINATION include )
Run Code Online (Sandbox Code Playgroud)
外部项目的CMakeLists.txt如下所示:
cmake_minimum_required( VERSION 2.8 )
project( exceptions )
add_library( exceptions SHARED exceptions.cc )
install( TARGETS …
Run Code Online (Sandbox Code Playgroud) 所以我知道我可以做这样的事情:
copy:
dest: /etc/issue
content: |
Hello
World
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
vars:
login_banner_text: !!str |-
"Hello\nWorld"
tasks:
- name: Set TTY login banner
copy:
dest: /etc/issue
content: "{{ login_banner_text }}"
Run Code Online (Sandbox Code Playgroud)
换行符直接打印到文件中,无需解析,即它是填充有\n
字符串的单行。我想在不将文件复制到位的情况下执行此操作,因为我需要将此文本复制到两个文件中。
对于一个文件,\n
字符串需要保持未解析状态,因此将其作为一行写入文件。对于另一个,我希望\n
被解释,以便将文本扩展为多行。
正在使用ini_file
模块修改第一个文件。此任务使用\n
变量声明中的显式按预期工作。
- name: "Set message"
ini_file:
dest: /etc/dconf/db/gdm.d/00-security-settings
section: org/gnome/login-screen
option: banner-message-text
value: string '{{ login_banner_text }}'
create: yes
tags:
- always
Run Code Online (Sandbox Code Playgroud)
但是,其他模块也有这种行为。
如果我将文件复制到位,则必须在两个地方维护相当长的文本(不是“Hello World”)。
根据这篇文章,我发现,我认为这是一种更好的方法。它将横幅存储在一个单独的文件中,然后使用它来修改两个配置文件。所以这个值只存储在一个地方。但是,@larsks 给出的答案确实回答了最初提出的问题。
- hosts: 127.0.0.1
connection: local
vars:
login_banner_text: "{{ lookup('file', 'login_banner.txt') …
Run Code Online (Sandbox Code Playgroud)