Executing python program

烈酒焚心 提交于 2019-12-01 18:36:05

When invoking python3 directly, python runs the script file you told it to, without using $PATH to find it. PYTHONPATH is irrelevant--that's used for searching for Python modules.

I'm guessing you're having issues with the wrong interpreter getting invoked when you run script.py by itself. I don't know what the first line of your script is, but it should be this:

#!/usr/bin/env python3

Or if you need even finer control:

#!/usr/bin/env python3.2

And for Python 2 scripts:

#!/usr/bin/env python2

Or:

#!/usr/bin/env python2.7

You should check that these executables exist on your system before trying to use them.

I would guess that path variables are ignored when python searches for the input-file. Python starts searching for 'script.py' in the current directory, not knowing that there is a path variable declared for that file, and therefore cannot find it.

Unfortunately I'm not sure how to solve it but maybe someone more experienced with variables can enlighten us?

python3 $(type -P script.py)

Tells Bash to look in the PATH for the executable file and supply its location and name.

For example:

$ type -P script.py
/usr/local/bin/script.py

To avoid duplicate entries in the path, you can do:

for dir in Python Bash; do
  dir_to_add="$HOME/$dir/scripts"
  case ":$PATH:" in
    *:"$dir_to_add":*) ;; # path already contains dir, do nothing
    *) PATH+=":$dir_to_add" ;;
  esac
done
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!