简体   繁体   English

如何在压缩标准lib时运行pycharm unittests?

[英]How to run pycharm unittests when standard lib is zipped?

I've been trying to port a Maya based python project over to PyCharm but I'm having trouble running unit tests. 我一直在尝试将基于Maya的python项目移植到PyCharm,但是我在运行单元测试时遇到了麻烦。

Maya provides its own python interpreter (mayapy.exe) with a zipped version of the python stdlib (in this case, 'Python27.zip') AFAIK there's nothing special about the stdlib here, but to run the native maya functions you have to use MayaPy rather than a generic python. Maya提供了自己的python解释器(mayapy.exe),其中包含python stdlib的压缩版本(在本例中为'Python27.zip')AFAIK这里没有关于stdlib的特别之处,但要运行本机maya函数,你必须使用它MayaPy而不是通用的python。

The problem appears to be that the jetBrains test runner (utRunner.py) wants to get os.system and it's barfing because it uses a specific import routine that doesn't allow for zip files. 问题似乎是jetBrains测试运行器(utRunner.py)想要获得os.system并且它正在进行barfing,因为它使用了一个不允许zip文件的特定导入例程。 It tries this: 它尝试这个:

def import_system_module(name):
  if sys.platform == "cli":    # hack for the ironpython
      return __import__(name)
  f, filename, desc = imp.find_module(name)
  return imp.load_module('pycharm_' + name, f, filename, desc)

and fails with this error: 并失败并出现此错误:

 ImportError: No module named os

I think because this is bypassing the zip import hook. 我认为因为这绕过了zip导入钩子。

There's one solution posted here , which is basically to unzip the standard library zip. 这里有一个解决方案 ,基本上解压缩标准库zip。 I'm reluctant to do that because I might need to run the tests on machines where I don't have admin rights. 我不愿意这样做,因为我可能需要在没有管理员权限的机器上运行测试。 I'm also reluctant to patch the code above since I'm not clear how it fits in to the whole test process. 我也不愿意修补上面的代码,因为我不清楚它是如何适应整个测试过程的。

So: how to run tests with a zipped standardlib using PyCharm, without unzipping the library or tweaking the PyCharm install too much? 那么:如何使用PyCharm使用压缩的standardlib运行测试,而无需解压缩库或调整PyCharm安装太多?

For lurkers: I was unable to find a better solution than the one linked above, so it was necessary to unzip the 2.7 standard libary into a loose folder. 对于潜伏者:我无法找到比上面链接的更好的解决方案,因此有必要将2.7标准库解压缩到一个松散的文件夹中。 Inelegant, but it works,. 不雅,但它的工作原理。

There was a further wrinkle that maya users need to watch out for: PyCharm does not like tests which run Maya.standalone -- the standalone session did not exit properly, so when running tests (in onr ore more files) that called 可能需要注意的另一个问题是:PyCharm不喜欢运行Maya.standalone的测试 - 独立会话没有正确退出,因此在运行测试时(在更多文件中)

 import maya.standalone
 maya.standalone.initialize()

The pycharm test runner would hang on completion. pycharm测试运行器将在完成时挂起。 After much frustration I found that adding an atexit handler to the test code would allow the standalone to exit in a way that PyCharm could tolerate: 经过多次挫折之后,我发现在测试代码中添加一个atexit处理程序将允许独立程序以PyCharm可以容忍的方式退出:

def get_out_of_maya():
    try:
       import maya.commands as cmds
       cmds.file(new=True, force=True)
    except:
       pass
    os._exit(0)   # note underscore

import atexit
atexit.register(get_out_of_maya)

This pre-empts the atexit hook in Maya and allows the tests to complete to the satisfaction of the Pycharm runner. 这会抢占Maya中的atexit钩子并允许测试完成,以满足Pycharm跑步者的需求。 FWIW, it also helps if you are running MayaPy.exe from a subprocess and executing your tests that way. FWIW,如果您从子进程运行MayaPy.exe并以这种方式执行测试,它也会有所帮助。

I ended up just editing Pycharm's utrunner.py file. 我最后只是编辑了Pycharm的utrunner.py文件。 It already imports os at the top of the file, so I'm not sure why it calls import_system_module. 它已经在文件的顶部导入了os,所以我不确定它为什么调用import_system_module。 The import command automatically handles zip files. import命令自动处理zip文件。 Also if you put the maya.standalone in the runner file, you don't need to call it in any of your test files. 此外,如果您将maya.standalone放在runner文件中,则无需在任何测试文件中调用它。

#os = import_system_module("os")
#re = import_system_module("re")
import re
try:
  import maya.standalone
  maya.standalone.initialize()
except ImportError:
  pass

I'm using Pycharm 5.0.1. 我正在使用Pycharm 5.0.1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM