【问题标题】:subprocess.Popen: 'OSError: [Errno 13] Permission denied' only on Linuxsubprocess.Popen: 'OSError: [Errno 13] Permission denied' 仅在 Linux 上
【发布时间】:2016-09-29 18:18:09
【问题描述】:

自提出问题以来,代码和日志发生了很大变化(由于重大改写)。

当我的代码(如下所示)在 Windows(我的笔记本电脑和 AppVeyor CI)上执行时,它会做它应该做的事情。但是在 Linux(TravisCI 上的 VM)上,它会抛出一个权限被拒绝的错误。


错误:

$ sudo python3 test.py
Testing espeak4py
Testing wait4prev
Traceback (most recent call last):
  File "test.py", line 10, in <module>
    mySpeaker.say('Hello, World!')
  File "/home/travis/build/sayak-brm/espeak4py/espeak4py/__init__.py", line 35, in say
    self.prevproc = subprocess.Popen(cmd, executable=self.executable, cwd=os.path.dirname(os.path.abspath(__file__)))
  File "/usr/lib/python3.2/subprocess.py", line 745, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.2/subprocess.py", line 1361, in _execute_child
    raise child_exception_type(errno_num, err_msg)
OSError: [Errno 13] Permission denied
The command "sudo python3 test.py" exited with 1.

代码:

espeak4py/init.py:

#! python3
import subprocess
import os
import platform

class Speaker:
    def __init__(self, voice="en", wpm=120, pitch=80):
        self.prevproc = None
        self.voice = voice
        self.wpm = wpm
        self.pitch = pitch
        if platform.system() == 'Windows': self.executable = os.path.dirname(os.path.abspath(__file__)) + "/espeak.exe"
        else: self.executable = os.path.dirname(os.path.abspath(__file__)) + "/espeak"

    def generateCmd(self, phrase):
        cmd = [
            self.executable,
            "--path=.",
            "-v", self.voice,
            "-p", self.pitch,
            "-s", self.wpm,
            phrase
        ]
        cmd = [str(x) for x in cmd]
        return cmd

    def say(self, phrase, wait4prev=False):
        cmd=self.generateCmd(phrase)
        if wait4prev:
            try: self.prevproc.wait()
            except AttributeError: pass
        else:
            try: self.prevproc.terminate()
            except AttributeError: pass
        self.prevproc = subprocess.Popen(cmd, executable=self.executable, cwd=os.path.dirname(os.path.abspath(__file__)))

test.py:

#! python3
import espeak4py
import time

print('Testing espeak4py\n')
print('Testing wait4prev')

mySpeaker = espeak4py.Speaker()

mySpeaker.say('Hello, World!')
time.sleep(1)
mySpeaker.say('Interrupted!')
time.sleep(3)

mySpeaker.say('Hello, World!')
time.sleep(1)
mySpeaker.say('Not Interrupted.', wait4prev=True)
time.sleep(5)

print('Testing pitch')

myHighPitchedSpeaker = espeak4py.Speaker(pitch=120)
myHighPitchedSpeaker.say('I am a demo of the say function')
time.sleep(5)

print('Testing wpm')

myFastSpeaker = espeak4py.Speaker(wpm=140)
myFastSpeaker.say('I am a demo of the say function')
time.sleep(5)

print('Testing voice')

mySpanishSpeaker = espeak4py.Speaker(voice='es')
mySpanishSpeaker.say('Hola. Como estas?')

print('Testing Completed.')

我不明白为什么它只能在一个平台上运行,而不能在另一个平台上运行。

Travis CI 日志:https://travis-ci.org/sayak-brm/espeak4py

AppVeyor 日志:https://ci.appveyor.com/project/sayak-brm/espeak4py

GitHub:https://sayak-brm.github.io/espeak4py


我得到了@zvone 推荐的ls -l 的输出:

$ ls -l
total 48
-rw-rw-r-- 1 travis travis   500 Sep 29 20:14 appveyor.yml
drwxrwxr-x 3 travis travis  4096 Sep 29 20:14 espeak4py
-rw-rw-r-- 1 travis travis 32400 Sep 29 20:14 LICENSE.md
-rw-rw-r-- 1 travis travis  2298 Sep 29 20:14 README.md
-rw-rw-r-- 1 travis travis     0 Sep 29 20:14 requirements.txt
-rw-rw-r-- 1 travis travis   759 Sep 29 20:14 test.py

$ ls -l espeak4py
total 592
-rw-rw-r-- 1 travis travis 276306 Sep 29 20:14 espeak
drwxrwxr-x 5 travis travis   4096 Sep 29 20:14 espeak-data
-rw-rw-r-- 1 travis travis 319488 Sep 29 20:14 espeak.exe
-rw-rw-r-- 1 travis travis   1125 Sep 29 20:14 __init__.py

【问题讨论】:

  • ls -l espeak 说什么?你有权限执行吗?
  • @zvone 将在下次提交后检查。
  • @zvone 给出了ls -l 的输出。

标签: python linux python-3.x cross-platform travis-ci


【解决方案1】:

这是您尝试运行的可执行文件:

-rw-rw-r-- 1 travis travis 276306 Sep 29 20:14 espeak

它的权限是rw-所有者(travis)读+写,rw-组(travis)读+写,r--其他人读。没有权限为任何人执行。

您必须向运行脚本的用户授予x(执行)权限。或者送给大家:

chmod 775 espeak

之后,ls- l 应该说:

-rwxrwxr-x 1 travis travis 276306 Sep 29 20:14 espeak

【讨论】:

  • 好吧,我不知道该把它放在哪里,因为它与代码有关,所以我把它放在这里。我想我可以把它放在 Ask Ubuntu 上。
  • 检查您的解决方案...顺便说一句,当我在 Windows 中开发软件时,我可以在我的 PC 上修改 Linux 权限还是必须使用安装脚本?
  • chmod 没有回应任何东西,所以我假设它有效。但在我的脚本中,我现在得到FileNotFoundError: [Errno 2] No such file or directory: '/home/travis/build/sayak-brm/espeak4py/espeak4py/espeak'。这正是espeak 应该在的位置。
  • ls -l espeak4py 给-rwxr-xr-x 1 travis travis 276306 Sep 30 04:09 espeak
  • 你必须调试这个。缺少信息。也许完整路径是错误的,也许目录上缺少权限。在同一件事上尝试'ls -l /home/travis/build/sayak-brm/espeak4py/espeak4py/espeak'. Try printing the cwd + executable in the script, try os.path.exists`。
猜你喜欢
  • 2020-06-21
  • 1970-01-01
  • 2016-08-07
  • 2017-07-15
  • 2015-11-26
  • 1970-01-01
  • 2020-07-29
  • 2012-06-16
相关资源
最近更新 更多