Member Avatar for MarcusMaximus

Hi,

I'm starting to code in python and would like some advice. I'm fairly competent in coding and have been doing alot of VB and shell scripting lately but as python is cross platform I've decided to switch.

How do I get python to run programs I have saved in my scripts folder ~/python/scripts
or My Documents on windows. I havn't decided what paltform do use to devlop in yet so any advice on this would be great too.

I've already set my system path to python and the interpreter runs fine and everything but I can't seem to run my scripts from the interpreter in the way some many tutorials do.

The commands "python 'c:\locatio\of\my\scripts\script.py" and "python '/home/me/python/scripts/myscript.py'" run fine from cmd and shell but when running from the python interpeter trying to instruct python to run a helloworld.py script in my documents is a pain.

Is it possible to tell the interpeter when to look when running scripts.

If this makes no sense I'm sorry it's 2am where I am and I'm tired.

If you are running from Python2, you can do execfile command from IDLE, but easier is just open the file and push F5 from IDLE. I am fond myself on conText text editor in Windows and invoking running script externally. IDLE sometimes disturbs function of programs, so at end it is good to run the program from command line directly/double click.

Execfile was removed from Python3, there to do same you must use exec(open('file.py').read())

Member Avatar for MarcusMaximus

If you are running from Python2, you can do execfile command from IDLE, but easier is just open the file and push F5 from IDLE. I am fond myself on conText text editor in Windows and invoking running script externally. IDLE sometimes disturbs function of programs, so at end it is good to run the program from command line directly/double click.

Execfile was removed from Python3, there to do same you must use exec(open('file.py').read())

Is the only way to run a file from the interpreter by typing the whole file path
e.g. execfile('C:\Users\my.user\Documents\MyScripts\MyPythonScripts\Test\helloworld.py')

Can I tell python to look in C:\Users\my.user\Documents\MyScripts\MyPythonScripts and all sub directories for scripts to run or should I save my scripts in c:\python27\ or some sub folder.

Or should I just open the script in IDLE and use the run function.

Member Avatar for MarcusMaximus

The thing thats confusing me is some tutorials have you write a script and save it, e.g. helloworld.py, and then tell you go to IDLE and just type helloworld.py to call your script. They do this without telling you how to point python to the directory you've saved your scripts in or if you should be saving your scripts in a certain folder.

Other tutorials have you import a script you've written also without telling you where you should have saved it, like
Dive Into Python

Generally it does not matter where as long that your files are in one directory. If you prepare scripts you want to be available systemwide, you can define them in directory you put in your PATH or PYTHONPATH variable. Then the script can be run by MYSCRIPT.PY at command line (CMD), if it is at path or current directory, otherwise you can add -m switch to PYTHON command if your prefered PYTHON is at path or current directory. You can also just double click the file if you include one input stattement as last command of your script.

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.