English 中文(简体)
补假方案
原标题:Executing python program

现在,我一直在寻找答案,但是这给我带来了真正的头痛:

I am using Ubuntu 12.04 and I want to execute a Python script from the terminal without using the full path. So i added /home/kyril/python/scripts/ to the PATH variable through putting the following into ./bashrc:

kyrilpathvariable="/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/kyril/Python/scripts/:/home/kyril/Bash/scripts"

if [ "$kyrilpathvariable" = "$PATH" ]; then
    echo PATH already exported

else

PATH=$PATH:/home/kyril/Python/scripts/
PATH=$PATH:/home/kyril/Bash/scripts/
export PATH

fi

(我知道这一条款是否没有必要,但我不想在我的PATH中作两度,如果我打上外衣的话。)

现在的问题是:这完全适用于我的印巴什文字,因此,在通过薄膜可加以执行之后,只能打上<代码>$ script.sh,执行。 然而,如果I类$ python3script.py 出现以下错误:python3:可开立档案。 [原文] 2)无此类档案或目录

如果我在通向文字的整个道路上打上了字,它就能够运作。 任何人都知道,我是错了什么? 我是否必须增加PYTHONPATH的目录? (我的理解是,这只有助于进口模块。)

感谢gu!

最佳回答

当直接援引<条码>python3时,python将你告知它,没有使用<条码>$PATH。 PYTHONPATH 是用于搜索沙捞模块的不相干的。

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

或者如果你需要更严厉的控制的话:

#!/usr/bin/env python3.2

并且用2个手稿:

#!/usr/bin/env python2

或:

#!/usr/bin/env python2.7

你们在试图利用这些迫害者之前,应当加以检查。

问题回答

我想,当花时间搜索投入文件时,道路变量就会被忽视。 灰 Python开始在目前的目录中寻找文字,而不知道该档案有一条路标,因此无法找到。

不幸的是,我不敢肯定如何解决该问题,但也许更有经验的变数人能够告诉我们?

python3 $(type -P script.py)

Tells Bash to look in the PATH for the executable file and provide its place and name.

例如:

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

为了避免道路上的重复,你可以:

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




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签