如何 运行 Ipython 中的 .py 文件?
How to run a .py file in Ipython?
Python 和 Ipython 版本:
- 笔记本服务器的版本是
3.0.0-f75fda4
。
- Python
2.7.9
|Anaconda 2.2.0
(64 位)|
问题描述
我正在 Python 中使用 undaqtools
模块。包页面是 here。该软件包包含将数据采集文件 (DAQ) 从驾驶模拟器输出转换为 HDF5 格式的功能。根据包页面,有两种方法可以做到这一点。一种方法是使用函数 daq.read
和 daq.write_hd5
一个一个地转换文件。我已经用过几次了,效果很好。第二种方法是使用脚本 undaq.py
同时批量转换多个 DAQ 文件。此脚本位于 C 盘中的 /Anaconda/Scripts/
(Windows 7)。我在 DrivingSimulator/Data
文件夹中有 3 个 DAQ 文件,名称为:
- Cars_20160601_01
- Cars_20160601_02
- Cars_20160601_03
所以,我首先将目录更改为DrivingSimulator/Data
。然后根据包的Getting Started页面,尝试了IPython中的undaq.py *
命令,报错:
%run C:/Users/durraniu/Anaconda/Scripts/undaq.py *
usage: undaq.py [-h] [-n NUMCPU] [-o OUTTYPE] [-e ELEMFILE] [-r] [-d] path
undaq.py: error: unrecognized arguments: Cars_20160601_02.daq Cars_20160601_03.daq
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
这是完整的回溯:
%tb
---------------------------------------------------------------------------
SystemExit Traceback (most recent call last)
C:\Users\durraniu\Anaconda\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc, compiler)
205 filename = fname
206 compiler = compiler or compile
--> 207 exec(compiler(scripttext, filename, 'exec'), glob, loc)
208
209 else:
C:\Users\durraniu\Anaconda\lib\site-packages\undaqtools-0.2.3-py2.7.egg\EGG-INFO\scripts\undaq.py in <module>()
2 # EASY-INSTALL-SCRIPT: 'undaqtools==0.2.3','undaq.py'
3 __requires__ = 'undaqtools==0.2.3'
----> 4 __import__('pkg_resources').run_script('undaqtools==0.2.3', 'undaq.py')
C:\Users\durraniu\Anaconda\lib\site-packages\setuptools-18.4-py2.7.egg\pkg_resources\__init__.py in run_script(self, requires, script_name)
733 ns.clear()
734 ns['__name__'] = name
--> 735 self.require(requires)[0].run_script(script_name, ns)
736
737 def __iter__(self):
C:\Users\durraniu\Anaconda\lib\site-packages\setuptools-18.4-py2.7.egg\pkg_resources\__init__.py in run_script(self, script_name, namespace)
1657 )
1658 script_code = compile(script_text, script_filename,'exec')
-> 1659 exec(script_code, namespace, namespace)
1660
1661 def _has(self, path):
C:\Users\durraniu\Anaconda\lib\site-packages\undaqtools-0.2.3-py2.7.egg\EGG-INFO\scripts\undaq.py in <module>()
C:\Users\durraniu\Anaconda\lib\argparse.pyc in parse_args(self, args, namespace)
1702 if argv:
1703 msg = _('unrecognized arguments: %s')
-> 1704 self.error(msg % ' '.join(argv))
1705 return args
1706
C:\Users\durraniu\Anaconda\lib\argparse.pyc in error(self, message)
2372 """
2373 self.print_usage(_sys.stderr)
-> 2374 self.exit(2, _('%s: error: %s\n') % (self.prog, message))
C:\Users\durraniu\Anaconda\lib\argparse.pyc in exit(self, status, message)
2360 if message:
2361 self._print_message(message, _sys.stderr)
-> 2362 _sys.exit(status)
2363
2364 def error(self, message):
SystemExit: 2
我无法理解这个错误。另外,我尝试在 CMD 中使用 undaq.py
但是打开了一个新的 window 说 Windows 无法打开这个文件:
请告诉我我做错了什么?另外,请注意 Script
文件夹和 Python 的路径已经在系统变量的 PATH 变量中。
更新:
按照@hpaulj 的说明,我做了以下操作:
## Changing to the directory containing DAQ files:
%cd C:/Users/durraniu/Documents/DrivingSimulator/Data
## Running the undaq.py script:
%run C:/Users/durraniu/Anaconda/Scripts/undaq.py -r -d \*
这给了我以下输出:
Glob Summary
--------------------------------------------------------------------
hdf5
daq size (KB) exists
--------------------------------------------------------------------
--------------------------------------------------------------------
debug = True
rebuild = True
Converting daqs with 1 cpus (this may take awhile)...
Debug Summary
Batch processing completed.
--------------------------------------------------------------------
Conversion Summary
--------------------------------------------------------------------
Total elapsed time: 0.1 s
Data converted: 0.000 MB
Data throughput: 0.0 MB/s
--------------------------------------------------------------------
似乎脚本无法 'see' Data 目录中的任何文件。我在带有前缀 python
的 cmd 中尝试了相同的操作,结果相同。我该如何解决这个问题?
供您参考,我将 undaq.py
文件的内容粘贴到此处:
#!C:\Users\durraniu\Anaconda\python.exe
# EASY-INSTALL-SCRIPT: 'undaqtools==0.2.3','undaq.py'
__requires__ = 'undaqtools==0.2.3'
__import__('pkg_resources').run_script('undaqtools==0.2.3', 'undaq.py')
请注意,我安装了 undaqtools 0.2.3 版本。
更新 2
我也试过在Ipython中关注:
%run -G C:/Users/durraniu/Anaconda/Scripts/undaq.py -r -d *
这会重复出现错误:
你不应该 运行宁 undaq
在 ipython。它是一个独立的脚本。
要运行一个python程序你需要使用python解释器。 python 的典型安装将 .py 文件与 python 解释器相关联。然而,在您的系统上似乎没有发生这种情况。
检查您是否已通过 运行ning python
在命令行提示符下安装 python(您应该获得一些关于 python 版本的信息,以及>>>
提示)。从 python 退出然后输入:
python undaq.py *
这会加载 python 解释器并告诉它读取并执行文件 undaq.py
中的命令,将当前目录中的所有文件作为参数传递。
用一个简单的脚本,echo_argv.py
:
import sys
print(sys.argv)
运行 在 Ipython 中使用您的命令行:
In [1222]: %run echo_argv *
['echo_argv.py', 'stack38002264.py', 'stack37714032.py', 'test', 'stack37930737.py ...]
表明它获得了 sys.argv
中的完整目录列表。
您的第一个错误是由 undaq.py
的命令行解析器产生的(因此是 argparse
错误堆栈)。
usage: undaq.py [-h] [-n NUMCPU] [-o OUTTYPE] [-e ELEMFILE] [-r] [-d] path
undaq.py: error: unrecognized arguments: Cars_20160601_02.daq Cars_20160601_03.daq
此消息告诉我 undaq
需要一个 path
参数,第一个文件名满足该参数:“160601_02.daq”。其他的文件名都是多余的,它在抱怨。
我怀疑如果只给它一个文件名或当前目录名,它会 运行 更好,例如:
%run undaq.py .
%run undaq.py Cars_20160601_01.daq
我希望你在 CMD 中得到同样的错误
python undaq.py *
我不明白为什么 1getting started
页面在命令行解析器给出此错误消息时建议使用 *
。我想知道是否存在版本差异 - 文档页面适用于不同的版本。
考虑
python undaq.py -h
%run undaq.py -h
获得更全面的帮助信息。
https://github.com/rogerlew/undaqTools/blob/master/undaqTools/scripts/undaq.py
查看这段代码,path
参数的 argparse
定义是
parser.add_argument('path', type=str, help='Path for glob ("*")')
这表明正确的调用是引用 '*'
因此它按原样传递给脚本,而不是由 shell
求值
python undaq.py '*'
但是我在 %run
.
中无法获得相同的行为
undaq.py
做它自己的 glob
,所以它需要一个路径字符串,而不是 shell glob
.
生成的完整列表
以下是 %run
关于通配符的说法:
Arguments are expanded using shell-like glob match. Patterns
'*', '?', '[seq]' and '[!seq]' can be used. Additionally,
tilde '~' will be expanded into user's home directory. Unlike
real shells, quotation does not suppress expansions. Use
*two* back slashes (e.g. ``\*``) to suppress expansions.
To completely disable these expansions, you can use -G flag.
所以我需要做:
In [1236]: %run echo_argv \*
['echo_argv.py', '*']
In [1237]: %run -G echo_argv *
['echo_argv.py', '*']
Python 和 Ipython 版本:
- 笔记本服务器的版本是
3.0.0-f75fda4
。 - Python
2.7.9
|Anaconda2.2.0
(64 位)|
问题描述
我正在 Python 中使用 undaqtools
模块。包页面是 here。该软件包包含将数据采集文件 (DAQ) 从驾驶模拟器输出转换为 HDF5 格式的功能。根据包页面,有两种方法可以做到这一点。一种方法是使用函数 daq.read
和 daq.write_hd5
一个一个地转换文件。我已经用过几次了,效果很好。第二种方法是使用脚本 undaq.py
同时批量转换多个 DAQ 文件。此脚本位于 C 盘中的 /Anaconda/Scripts/
(Windows 7)。我在 DrivingSimulator/Data
文件夹中有 3 个 DAQ 文件,名称为:
- Cars_20160601_01
- Cars_20160601_02
- Cars_20160601_03
所以,我首先将目录更改为DrivingSimulator/Data
。然后根据包的Getting Started页面,尝试了IPython中的undaq.py *
命令,报错:
%run C:/Users/durraniu/Anaconda/Scripts/undaq.py *
usage: undaq.py [-h] [-n NUMCPU] [-o OUTTYPE] [-e ELEMFILE] [-r] [-d] path
undaq.py: error: unrecognized arguments: Cars_20160601_02.daq Cars_20160601_03.daq
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
这是完整的回溯:
%tb
---------------------------------------------------------------------------
SystemExit Traceback (most recent call last)
C:\Users\durraniu\Anaconda\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc, compiler)
205 filename = fname
206 compiler = compiler or compile
--> 207 exec(compiler(scripttext, filename, 'exec'), glob, loc)
208
209 else:
C:\Users\durraniu\Anaconda\lib\site-packages\undaqtools-0.2.3-py2.7.egg\EGG-INFO\scripts\undaq.py in <module>()
2 # EASY-INSTALL-SCRIPT: 'undaqtools==0.2.3','undaq.py'
3 __requires__ = 'undaqtools==0.2.3'
----> 4 __import__('pkg_resources').run_script('undaqtools==0.2.3', 'undaq.py')
C:\Users\durraniu\Anaconda\lib\site-packages\setuptools-18.4-py2.7.egg\pkg_resources\__init__.py in run_script(self, requires, script_name)
733 ns.clear()
734 ns['__name__'] = name
--> 735 self.require(requires)[0].run_script(script_name, ns)
736
737 def __iter__(self):
C:\Users\durraniu\Anaconda\lib\site-packages\setuptools-18.4-py2.7.egg\pkg_resources\__init__.py in run_script(self, script_name, namespace)
1657 )
1658 script_code = compile(script_text, script_filename,'exec')
-> 1659 exec(script_code, namespace, namespace)
1660
1661 def _has(self, path):
C:\Users\durraniu\Anaconda\lib\site-packages\undaqtools-0.2.3-py2.7.egg\EGG-INFO\scripts\undaq.py in <module>()
C:\Users\durraniu\Anaconda\lib\argparse.pyc in parse_args(self, args, namespace)
1702 if argv:
1703 msg = _('unrecognized arguments: %s')
-> 1704 self.error(msg % ' '.join(argv))
1705 return args
1706
C:\Users\durraniu\Anaconda\lib\argparse.pyc in error(self, message)
2372 """
2373 self.print_usage(_sys.stderr)
-> 2374 self.exit(2, _('%s: error: %s\n') % (self.prog, message))
C:\Users\durraniu\Anaconda\lib\argparse.pyc in exit(self, status, message)
2360 if message:
2361 self._print_message(message, _sys.stderr)
-> 2362 _sys.exit(status)
2363
2364 def error(self, message):
SystemExit: 2
我无法理解这个错误。另外,我尝试在 CMD 中使用 undaq.py
但是打开了一个新的 window 说 Windows 无法打开这个文件:
请告诉我我做错了什么?另外,请注意 Script
文件夹和 Python 的路径已经在系统变量的 PATH 变量中。
更新:
按照@hpaulj 的说明,我做了以下操作:
## Changing to the directory containing DAQ files:
%cd C:/Users/durraniu/Documents/DrivingSimulator/Data
## Running the undaq.py script:
%run C:/Users/durraniu/Anaconda/Scripts/undaq.py -r -d \*
这给了我以下输出:
Glob Summary
--------------------------------------------------------------------
hdf5
daq size (KB) exists
--------------------------------------------------------------------
--------------------------------------------------------------------
debug = True
rebuild = True
Converting daqs with 1 cpus (this may take awhile)...
Debug Summary
Batch processing completed.
--------------------------------------------------------------------
Conversion Summary
--------------------------------------------------------------------
Total elapsed time: 0.1 s
Data converted: 0.000 MB
Data throughput: 0.0 MB/s
--------------------------------------------------------------------
似乎脚本无法 'see' Data 目录中的任何文件。我在带有前缀 python
的 cmd 中尝试了相同的操作,结果相同。我该如何解决这个问题?
供您参考,我将 undaq.py
文件的内容粘贴到此处:
#!C:\Users\durraniu\Anaconda\python.exe
# EASY-INSTALL-SCRIPT: 'undaqtools==0.2.3','undaq.py'
__requires__ = 'undaqtools==0.2.3'
__import__('pkg_resources').run_script('undaqtools==0.2.3', 'undaq.py')
请注意,我安装了 undaqtools 0.2.3 版本。
更新 2
我也试过在Ipython中关注:
%run -G C:/Users/durraniu/Anaconda/Scripts/undaq.py -r -d *
这会重复出现错误:
你不应该 运行宁 undaq
在 ipython。它是一个独立的脚本。
要运行一个python程序你需要使用python解释器。 python 的典型安装将 .py 文件与 python 解释器相关联。然而,在您的系统上似乎没有发生这种情况。
检查您是否已通过 运行ning python
在命令行提示符下安装 python(您应该获得一些关于 python 版本的信息,以及>>>
提示)。从 python 退出然后输入:
python undaq.py *
这会加载 python 解释器并告诉它读取并执行文件 undaq.py
中的命令,将当前目录中的所有文件作为参数传递。
用一个简单的脚本,echo_argv.py
:
import sys
print(sys.argv)
运行 在 Ipython 中使用您的命令行:
In [1222]: %run echo_argv *
['echo_argv.py', 'stack38002264.py', 'stack37714032.py', 'test', 'stack37930737.py ...]
表明它获得了 sys.argv
中的完整目录列表。
您的第一个错误是由 undaq.py
的命令行解析器产生的(因此是 argparse
错误堆栈)。
usage: undaq.py [-h] [-n NUMCPU] [-o OUTTYPE] [-e ELEMFILE] [-r] [-d] path
undaq.py: error: unrecognized arguments: Cars_20160601_02.daq Cars_20160601_03.daq
此消息告诉我 undaq
需要一个 path
参数,第一个文件名满足该参数:“160601_02.daq”。其他的文件名都是多余的,它在抱怨。
我怀疑如果只给它一个文件名或当前目录名,它会 运行 更好,例如:
%run undaq.py .
%run undaq.py Cars_20160601_01.daq
我希望你在 CMD 中得到同样的错误
python undaq.py *
我不明白为什么 1getting started
页面在命令行解析器给出此错误消息时建议使用 *
。我想知道是否存在版本差异 - 文档页面适用于不同的版本。
考虑
python undaq.py -h
%run undaq.py -h
获得更全面的帮助信息。
https://github.com/rogerlew/undaqTools/blob/master/undaqTools/scripts/undaq.py
查看这段代码,path
参数的 argparse
定义是
parser.add_argument('path', type=str, help='Path for glob ("*")')
这表明正确的调用是引用 '*'
因此它按原样传递给脚本,而不是由 shell
python undaq.py '*'
但是我在 %run
.
undaq.py
做它自己的 glob
,所以它需要一个路径字符串,而不是 shell glob
.
以下是 %run
关于通配符的说法:
Arguments are expanded using shell-like glob match. Patterns
'*', '?', '[seq]' and '[!seq]' can be used. Additionally,
tilde '~' will be expanded into user's home directory. Unlike
real shells, quotation does not suppress expansions. Use
*two* back slashes (e.g. ``\*``) to suppress expansions.
To completely disable these expansions, you can use -G flag.
所以我需要做:
In [1236]: %run echo_argv \*
['echo_argv.py', '*']
In [1237]: %run -G echo_argv *
['echo_argv.py', '*']