Page 1 of 4

python path

Posted: 2. Feb 2012, 09:54
by ink3
I want run python script with:
python script
from directory /home/script-python
and how to add this directory to python path?

In script bash I put linie in .bashrc
export PATH=$PATH:/home/script-bash

Re: python path

Posted: 2. Feb 2012, 10:20
by gapan
There is no special "python path". You use the same PATH as with bash or anything else. And you run it without specifying the python interpreter at all, like any program. You need to make it executable first of course.

Re: python path

Posted: 2. Feb 2012, 10:57
by ink3
Not work.
I put:

export PATH=$PATH:/home/stahoo/skrypty_python/

and
stahoo[~]$ python losuj.py
python: can't open file 'losuj.py': [Errno 2] No such file or directory
but from directory works

Re: python path

Posted: 2. Feb 2012, 11:09
by gapan
As I said, don't run it with python. Just run the executable as any other executable.

Code: Select all

$ losuj.py
If you want to run it with python, you have to use the full path.

Re: python path

Posted: 2. Feb 2012, 12:04
by ink3
bash script hello
stahoo[~]$ hello
bash: /home/stahoo/skrypty_bash/hello: Brak dostepu (*access denied)
stahoo[~]$ ./hello
bash: ./hello: Nie ma takiego pliku ani katalogu (*No such file or directory)
stahoo[~]$ sh hello
hello world
stahoo[~]$ bash hello
hello world
python script losuj.py
$ losuj.py
bash: losuj.py: nie znaleziono polecenia (*command not found)

Re: python path

Posted: 2. Feb 2012, 12:43
by mimosa
What does the first line of your python script look like? Is it like this? If not, try putting this as the first line:

Code: Select all

#!/usr/bin/env python
Same thing for bash scripts:

Code: Select all

#!/bin/sh
In each case, these lines indicate that the files are scripts, and what to run them with - here, python or bash.

Secondly, are they executable?

Code: Select all

#chmod +x /foo/bar/lusaj.py

Re: python path

Posted: 2. Feb 2012, 12:53
by thenktor
Make it executable: chmod +x /home/stahoo/skrypty_bash/hello

Re: python path

Posted: 2. Feb 2012, 12:54
by thenktor
thenktor wrote:Make it executable: chmod +x /home/stahoo/skrypty_bash/hello
EDIT: And use export LANG=C before posting error messages.

Re: python path

Posted: 2. Feb 2012, 13:12
by ink3
losuj.py
#!/bin/python
import random
random.seed()
s=random.randint(1,595)
print s
I make new losuj-1.py
#!/usr/bin/env python
import random
random.seed()
s=random.randint(1,595)
print s
$ export LANG=C

$ losuj-1.py
bash: losuj-1.py: command not found
I always give chmod +x script

Re: python path

Posted: 2. Feb 2012, 14:56
by mimosa
Well, then I'm not sure what the problem is - and maybe the "env" shouldn't be there or isn't needed. I just copied from a script written by the devs to be sure it was right!

Are you sure the scripts directory is still in your $PATH? Check by doing

Code: Select all

echo $PATH
Sometimes the effects of changes created by export are unexpectedly patchy, but you can just reissue the command in the terminal concerned immediately before calling the script. Once you've got it working, you may want to change your .profile to include the scripts directory in $PATH.