質問

I'm trying to call a self-defined command line function in python. I defined my function using apple script in /.bash_profile as follows:

function vpn-connect  {
/usr/bin/env osascript <<-EOF
tell application "System Events"
        tell current location of network preferences
                set VPN to service "YESVPN" -- your VPN name here
                if exists VPN then connect VPN
                repeat while (current configuration of VPN is not connected)
                    delay 1
                end repeat
        end tell
end tell
EOF
}

And when I tested $ vpn-connect in bash, vpn-connect works fine. My vpn connection is good.

So I created vpn.py which has following code:

import os

os.system("echo 'It is running.'")
os.system("vpn-connect")

I run it with python vpn.py and got the following output:

vpn Choushishi$ python vpn.py 
It is running.
sh: vpn-connect: command not found

This proves calling self-defined function is somehow different from calling the ones that's pre-defined by the system. I have looked into pydoc os but couldn't find useful information.

役に立ちましたか?

解決

  1. A way would be to read the ./bash_profile before. As @anishsane pointed out you can do this:

    vpn=subprocess.Popen(["bash"],shell=True,stdin= subprocess.PIPE)
    vpn.communicate("source /Users/YOUR_USER_NAME/.bash_profile;vpn-connect")
    
  2. or with os.system

    os.system('bash -c "source /Users/YOUR_USER_NAME/.bash_profile;vpn-connect"')
    
  3. Or try

    import subprocess
    subprocess.call(['vpn-connect'], shell = True)
    
  4. and try

    import os
    os.system('bash -c vpn-connect')
    

    according to http://linux.die.net/man/1/bash

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top