简体   繁体   English

无法将子流程模块导入python3

[英]Can't import subprocess module into python3

Im trying to import subprocess. 我正在尝试导入子流程。 However Im unable to even import subprocess. 但是我什至无法导入子进程。

Currently, my file (throwaway.py) consists of only one line: 目前,我的文件(throwaway.py)仅包含一行:

import subprocess

but it returns the error: 但它返回错误:

Traceback (most recent call last):
  File "throwaway.py", line 1, in <module>
    import subprocess
ImportError: bad magic number in 'subprocess': b'\x03\xf3\r\n'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 12, in <module>
    import subprocess, tempfile, os.path, re, pwd, grp, os, time
ImportError: bad magic number in 'subprocess': b'\x03\xf3\r\n'

Original exception was:
Traceback (most recent call last):
  File "throwaway.py", line 1, in <module>
    import subprocess
ImportError: bad magic number in 'subprocess': b'\x03\xf3\r\n'

What are magic number errors? 什么是幻数错误? I read that they occur when you accidentally give a file the .pyc extention rather than .py? 我读到它们是在您不小心将文件扩展名为.pyc而不是.py时发生的?

In this case the error occurs because for some reason your code is importing Python 2.7 subprocess.pyc into Python 3. Python 2.7 .pyc s start with b'\\x03\\xf3\\r\\n' . 在这种情况下,因为某些原因,你的代码是进口的Python 2.7时出现错误subprocess.pyc成Python 3 Python的2.7 .pyc先从b'\\x03\\xf3\\r\\n' Perhaps you've created one virtualenv for both Python 2 and 3 (it wouldn't work ), or are using a wrong PYTHONPATH . 也许您已经为Python 2和3创建了一个virtualenv(它不起作用 ),或者使用了错误的PYTHONPATH

Use pyclean and try to import it again. 使用pyclean并尝试再次导入。

pyclean <path>

will remove all pyc files in path you'll provide (recursively), so there won't be compiled files, so no conflict. 会(递归)删除您提供的路径中的所有pyc文件,因此不会编译文件,因此不会发生冲突。

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

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