如何在Ipython中运行.py文件?

2024-05-14 18:11:01 发布

您现在位置:Python中文网/ 问答频道 /正文

Python和Ipython版本:

  1. 笔记本服务器的版本是3.0.0-f75fda4
  2. Python2.7.9Python2.2.0(64位)

问题描述

我在Python中使用undaqtools模块。包页是here。此软件包包含将数据采集文件(DAQ)从驾驶模拟器输出转换为HDF5格式的功能。根据包页,有两种方法可以这样做。一种方法是使用函数daq.readdaq.write_hd5逐个转换文件。我已经用过好几次了,效果很好。第二种方法是使用脚本undaq.py同时批量转换许多DAQ文件。此脚本位于C驱动器(Windows 7)中的/Anaconda/Scripts/中。我在DrivingSimulator/Data文件夹中有3个DAQ文件,名为:

  1. 汽车20160601
  2. 汽车-20160601-02
  3. 汽车20160601

所以,我首先将目录更改为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,但这打开了一个新窗口,表示Windows无法打开此文件:

enter image description here

请告诉我我做错了什么?另外,请注意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
--------------------------------------------------------------------

脚本似乎看不到数据目录中的任何文件。我在带有前缀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

我在伊普顿也试过以下几点:

%run -G C:/Users/durraniu/Anaconda/Scripts/undaq.py -r -d *

这将导致一个重复出现的错误:
enter image description here


Tags: 文件runinpyselfmessagedatalib

热门问题