假设我有以下脚本test.py:
import my_library
bar = 12
def foo():
nested_bar = 21
my_library.do_things()
def nested_foo():
nested_bar += 11
not_a_variable += 1
{$ invalid_syntax
bar = 13
foo()
bar = 14
Run Code Online (Sandbox Code Playgroud)
我很好奇当我跑步时到底会发生什么python test.py。显然,Python 不只是逐行读取程序 - 否则它不会在实际执行程序之前捕获语法错误。但这使得解释器的工作显得有些模糊。我想知道是否有人能帮我解决问题。我特别想知道:
Python 在什么时候意识到第 13 行有语法错误?
Python 在什么时候读取嵌套函数并将它们添加到 的作用域中foo?
foo同样,Python遇到函数时如何将其添加到其命名空间而不执行它?
假设my_library是无效的导入。ImportErrorPython在执行任何其他命令之前是否一定会引发?
假设my_library是一个有效的模块,但它没有功能do_things。Python 在执行期间或之前什么时候会意识到这一点foo()?
如果有人能给我提供有关 Python 如何解析和执行脚本的文档,我将非常感激。
当将 xkcd() 与 matplotpib 一起使用时,没有任何字体以通常的漫画字体显示。有什么改变或者我做错了什么吗?
x = df['Time']
y = df['Adjustment']
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax1 = fig.add_subplot(1,1,1)
ax1.plot(x,y)
ax1.xaxis.set_visible(False)
ax1.yaxis.set_visible(False)
plt.axvline(x=2.3, color='k', ls='dashed')
plt.axvline(x=6, color='k', ls='dashed')
ax.text(4,4,'Culture Shock', size=16)
plt.title('Test title')
plt.xkcd()
plt.show()
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助。
我应该澄清的是,该图将以 xkcd 样式绘制,而不是任何字体。它打印的内容与 Times New Roman 类似。
#! /usr/bin/python -tt
import os
def searchFile(path1,ext1,fileName1):
pathList = []
for root, dirs, files in os.walk(path1):
for file in files:
if file.endswith(ext1):
pathList.append(os.path.join(root,file))
print "-----The file is present under the below path------\n"
for ele in pathList:
if fileName1 in ele:
print ele
def main():
path = raw_input("Please enter the path you wish to spider. Also make sure that the files/subfolders have the correct permissions.\n")
ext = raw_input("Enter the extension you wish to search/ find. Eg: For class files enter .class …Run Code Online (Sandbox Code Playgroud) 我使用 pip3 安装了 ipython3。
pip3 install ipython[all]
Run Code Online (Sandbox Code Playgroud)
当我运行 python 时,我的 ~/.pythonrc.py 文件遇到错误(仅在 Python 3 上,不在 Python 2 上)。
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "/Users/Mike/.pythonrc.py", line 43, in <module>
readline.read_history_file(history_path)
OSError: [Errno 22] Invalid argument
Run Code Online (Sandbox Code Playgroud)
我在 stackoverflow 上找不到答案,但进行了一些挖掘并找到了解决方案。我不确定为什么现在有效或者是否有更好的解决方案。
在 .pythonrc.py 文件中:
try:
import readline
except ImportError:
pass
history_path = os.path.expanduser('~/.pyhistory')
if os.path.isfile(history_path):
readline.read_history_file(history_path)
atexit.register(lambda …Run Code Online (Sandbox Code Playgroud) 我尝试ipython qtconsole参考stackoverflow中的这个答案来更改字体大小;但是,无论我如何更改 .txt 文件,字体大小都拒绝更改~/.ipython/profile_default/ipython_config.py。
\xe2\x9e\x9c ~ ipython profile locate\n/home/nick/.ipython/profile_default\n\n\xe2\x9e\x9c ~ head .ipython/profile_default/ipython_config.py \n# Configuration file for ipython.\nc = get_config() \nc.IPythonWidget.font_size = 16\nc.IPythonWidget.font_family = \'Source Code Pro\'\n\n\xe2\x9e\x9c ~ uname -a\nLinux nick-thinkpad 4.2.5-1-ARCH #1 SMP PREEMPT Tue Oct 27 08:13:28 CET 2015 x86\n_64 GNU/Linux \n\n\xe2\x9e\x9c ~ ipython --version\n4.0.0\nRun Code Online (Sandbox Code Playgroud)\n\n令我惊讶的是,ipython qtconsole --ConsoleWidget.font_size=16有效。我的配置有什么问题吗?
我有相同的代码,我偶尔从命令行运行它,python source.py偶尔将其复制/粘贴到交互式 IPython 中。我想以两种方式执行稍微不同的代码,并尝试将差异嵌入到以下代码块中:
try:
__IPYTHON__
# do IPython-mode code
except NameError:
# do script-mode code
Run Code Online (Sandbox Code Playgroud)
我知道在 Python 中这是一种常见的技术,但它搞砸了 PyCharm 中的自动标记,我希望找到一些更有吸引力的方法来测试 IPython 的存在,也许通过测试'__IPYTHON__' in ...或类似的方法。然而,我看到这个符号既不在locals()也不在 中globals()。有没有其他地方可以测试它的存在,或者任何其他更常规的测试,而不直接使用异常?
我正在使用 Python 2.7.11 和 IPython 5.1.0。非常感谢。
更新相关但无帮助:How do I check if a variable isn’t?
你们中的一些人可能知道,您可以在任何 julia 脚本中使用 \xce\xbb、\xcf\x86、\xce\xb1,..。这对于Python来说不也是可能的吗?我会使用 julia,但是仍然有一些来自 python 的包我必须包装。
\n\n最好的祝愿
\n我正在尝试在我的代码中使用 ipython 提供的 Traitlets 库。假设如果一个特征是特定类的实例,我如何观察该类成员的值的变化。
例子:
class A:
def init(self,val):
self.value = val
class myApp(HasTraits):
myA = Instance(A,kw={'val':2})
Run Code Online (Sandbox Code Playgroud)
如果对象 myA 的“value”成员变量发生更改,我希望调用一个观察方法。如下所示:
@observe('myA.value')
def onValueChange(self,change):
return
Run Code Online (Sandbox Code Playgroud)
Trailets 的实施是否可行?
我确信已经有人回答过这个问题,但我找不到。
我有一个脚本test.py,其存在的唯一目的是将变量var放入全局命名空间中:
def test():
global var
var = 'stuff'
Run Code Online (Sandbox Code Playgroud)
test.py以下是我在 ipython 中使用运行%run,然后运行test(),然后尝试访问 时发生的情况var。
Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: %run test.py
In [2]: test()
In [3]: var
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-84ddba356ca3> in <module>()
----> 1 var …Run Code Online (Sandbox Code Playgroud) ipython ×10
python ×6
python-2.7 ×2
python-3.x ×2
jar ×1
julia ×1
jupyter ×1
macos ×1
matplotlib ×1
scripting ×1
traits ×1