python path

Other talk about Salix
User avatar
ink3
Posts: 80
Joined: 9. Apr 2011, 14:16

python path

Post 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
User avatar
gapan
Salix Wizard
Posts: 6354
Joined: 6. Jun 2009, 17:40

Re: python path

Post 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.
Image
Image
User avatar
ink3
Posts: 80
Joined: 9. Apr 2011, 14:16

Re: python path

Post 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
User avatar
gapan
Salix Wizard
Posts: 6354
Joined: 6. Jun 2009, 17:40

Re: python path

Post 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.
Image
Image
User avatar
ink3
Posts: 80
Joined: 9. Apr 2011, 14:16

Re: python path

Post 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)
User avatar
mimosa
Salix Warrior
Posts: 3311
Joined: 25. May 2010, 17:02
Contact:

Re: python path

Post 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
User avatar
thenktor
Salix Wizard
Posts: 2426
Joined: 6. Jun 2009, 14:47
Location: Franconia
Contact:

Re: python path

Post by thenktor »

Make it executable: chmod +x /home/stahoo/skrypty_bash/hello
Image
burnCDDA (burns audio CDs)
geBIERt (German beer blog)
User avatar
thenktor
Salix Wizard
Posts: 2426
Joined: 6. Jun 2009, 14:47
Location: Franconia
Contact:

Re: python path

Post by thenktor »

thenktor wrote:Make it executable: chmod +x /home/stahoo/skrypty_bash/hello
EDIT: And use export LANG=C before posting error messages.
Image
burnCDDA (burns audio CDs)
geBIERt (German beer blog)
User avatar
ink3
Posts: 80
Joined: 9. Apr 2011, 14:16

Re: python path

Post 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
User avatar
mimosa
Salix Warrior
Posts: 3311
Joined: 25. May 2010, 17:02
Contact:

Re: python path

Post 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.
Post Reply