简体   繁体   English

python脚本不会从命令行运行,但是在以“ python”开头时会运行

[英]python script won't run from command line but will when preceded by 'python'

I have written a script that I need to run twice a day. 我编写了一个脚本,每天需要运行两次。 My intention was to make the script and then run it as a cron job on the necessary schedule. 我的意图是制作脚本,然后在必要的时间表上将其作为cron作业运行。
I wrote the script in the spyder IDE, and it runs flawlessly from there. 我在spyder IDE中编写了脚本,并且可以从那里完美地运行。 I can also run it from the terminal by typing python python_daily_update.py . 我还可以从终端运行python python_daily_update.py来运行它。

I have added the #!/usr/bin/python statement to the front of the script and made it executable. 我已经在脚本的前面添加了#!/usr/bin/python语句并使其可执行。 When I attempt to run the script from the command line without the 'python' proceeding it, I get a errors as follows: 当我尝试从命令行运行脚本而没有继续执行“ python”命令时,出现如下错误:

$ python_daily_update.py
Failed to import the site module
Traceback (most recent call last):
  File "/usr/lib/python3.4/site.py", line 586, in <module>
    main()
  File "/usr/lib/python3.4/site.py", line 572, in main
    known_paths = addusersitepackages(known_paths)
  File "/usr/lib/python3.4/site.py", line 287, in addusersitepackages
    user_site = getusersitepackages()
  File "/usr/lib/python3.4/site.py", line 263, in getusersitepackages
    user_base = getuserbase() # this will also set USER_BASE
  File "/usr/lib/python3.4/site.py", line 253, in getuserbase
    USER_BASE = get_config_var('userbase')
  File "/usr/lib/python3.4/sysconfig.py", line 597, in get_config_var
    return get_config_vars().get(name)
  File "/usr/lib/python3.4/sysconfig.py", line 540, in get_config_vars
    _init_posix(_CONFIG_VARS)
  File "/usr/lib/python3.4/sysconfig.py", line 412, in _init_posix
    from _sysconfigdata import build_time_vars
  File "/usr/lib/python3.4/_sysconfigdata.py", line 6, in <module>
    from _sysconfigdata_m import *
ImportError: No module named '_sysconfigdata_m'

I'm running linux mint 17.1. 我正在运行linux mint 17.1。 I believe there is something wrong with the path but I can't seem to figure out what it is. 我相信这条路径有问题,但我似乎无法弄清楚这是什么。 Any ideas? 有任何想法吗?

At the command line execute: which python 在命令行执行:哪个python

Verify if the output is equal to which you wrote at the beginning of the script, if not just change it and it will work. 验证输出是否等于您在脚本开始时编写的输出,如果不只是对其进行更改,它将可以正常工作。

The script must be run like this: ./python_daily_update.py 该脚本必须这样运行:./python_daily_update.py

In linux/unix unless the executable is in your PATH you have to tell it where the file is, so ./python_daily_update.py tells the shell that the executable is in your current directory ie . 在linux / unix中,除非可执行文件在您的PATH否则您必须告诉它文件在哪里,因此./python_daily_update.py告诉Shell可执行文件在您的当前目录中,即.

The shebang does not point to your Anaconda install either so you also need to change that. shebang也不指向您的Anaconda安装,因此您还需要更改它。

I didn't observe which shell you are running, but the general rule is there will be an rc file that is loaded when the shell is loaded, and you should have a PATH statement in it that includes ./ as one of the search paths for executable files encountered in the scripts or programs found by the shell. 我没有观察到您正在运行哪个shell,但是一般规则是,在加载shell时会加载一个rc文件,并且您应该在其中包含PATH语句,其中包括./作为搜索路径之一用于外壳程序找到的脚本或程序中遇到的可执行文件。 If you don't have it, you will get the behavior you described. 如果没有它,您将得到您描述的行为。

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

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