i searched for similar threads but couldnt find any.
i write python scripts ..but i want to store them in someother folder eg /Desktop/...
the scripts get executed from the unix terminal..however the python intepreter(initialised by typing python in terminal window )is not able to find the files.
i changed the dir search through sys.path.append..but it still doesnt work...i also noticed i have to append this everytime i run the intepreter new..
is there ne way to make a dir addnt permanent.
am i supposed to make changes elsewhere?? WHERE AM I GOING WRONG??
errors:
ImportError: No module named sc.py(i made the necessary changes to run a module as a script)
NameError: name 'sc' is not defined
i have not been able to understNd this "PYTHONPATH" STUFF.
any added suggestions will be valuable

THANK YOU

Recommended Answers

All 6 Replies

Try placing the next line at the beginning of your python script:

#!/usr/bin/env python

Then just call the script from the terminal:

./path_to_script/script.py

What happens?

It's very simple. First python finds modules installed in some site-packages directories. For example /usr/lib/python2.7/site-packages or /usr/lib64/python2.7/site-packages if you have a 64 bits python, or your per-user site-packages directory ~/.local/lib/python2.7/site-packages (you can create it yourself if it does not exist). If you want python to find modules in other directories, for example ~/foo/bar and ~/baz/taz, you can add the following line to your ~/.bashrc

export PYTHONPATH="$HOME/foo/bar:$HOME/baz/taz:$HOME/.local/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages"

You can add as many directories as you need. Then restart a bash shell and import module sc if sc.py is in one of these directories.

@valorien: also read this as an alternative to the shebang line: http://www.daniweb.com/software-development/python/code/241988

commented: very informative +1

@valorien: also read this as an alternative to the shebang line: http://www.daniweb.com/software-development/python/code/241988

This sounds like a bad idea. Non-portable, and it means all your Python scripts have to end in .py (and if by chance you were to have an actual binary end in .py you wouldn't be able to use it). Is there some problem with using a shebang line that this approach is trying to solve?

This sounds like a bad idea. Non-portable, and it means all your Python scripts have to end in .py (and if by chance you were to have an actual binary end in .py you wouldn't be able to use it). Is there some problem with using a shebang line that this approach is trying to solve?

It's only a practical configuration of your linux system. It doesn't prevent you from adding a shebang line if you want. I've been using this trick for some time now and it is very nice. Why not use all the tools that come with a linux system ?

It's only a practical configuration of your linux system. It doesn't prevent you from adding a shebang line if you want. I've been using this trick for some time now and it is very nice. Why not use all the tools that come with a linux system ?

Well, I could pretty easily install software and graphics to make my desktop look just like Windows 7, and with some dedication I could rename all the binaries to end in .exe, and then there's some trickery that I could use to make all my paths appear to start with C: and use backslashes instead of forward slashes, if I really wanted my Linux system to act like Windows... but I have no idea why I'd want that. Similarly, I don't want my Linux system to assume filetypes based on extensions, because that's one of the things that often frustrates me in Windows. But no, your way is good, too. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.