How to run program in Linux

Jim Byrnes jf_byrnes at comcast.net
Sat Apr 17 17:34:57 CEST 2010


Dave Angel wrote:
> Jim Byrnes wrote:
>> Dave Angel wrote:
>>> Jim Byrnes wrote:
>>>> Dave Angel wrote:
>>>>> Jim Byrnes wrote:
>>>>>> <div class="moz-text-flowed" style="font-family: -moz-fixed">I am
>>>>>> just
>>>>>> learning Python and am new to Linux so I am probably doing something
>>>>>> to trip myself up. I am trying to run an example GUI program that
>>>>>> fetches a record from a database. All the files are in the same
>>>>>> folder.
>>>>>>
>>>>>> The program runs but its results vary depending on how I started it.
>>>>>> If I run it from the terminal or Idle, I enter a key and the program
>>>>>> fetches the proper record. If I run it from Nautilis or a panel
>>>>>> launcher, I enter the proper key and I get an error message saying
>>>>>> the
>>>>>> key does not exist.
>>>>>>
>>>>>> I am assuming that I am having path issues but don't know how to
>>>>>> correct it.
>>>>>>
>>>>>> Thamks, Jim
>>>>>>
>>>>> Presumably you're also new to mailing lists.
>>>>
>>>> Not really.
>>>>
>>>>> At an absolute minimum when you describe an error, PASTE the error
>>>>> message, complete with traceback, into your message. As it stands, I'm
>>>>> left wondering which key on your keyboard can possibly "not exist."
>>>>> Perhaps it's a non-ASCII code, and you're getting some encoding error.
>>>>> That's a common discrepancy between running from a terminal and
>>>>> running
>>>>> from some GUI.
>>>>
>>>> The error was generated by the program, not Python. The 'key' I was
>>>> referring to was a dictionary type key, not a physical one on the
>>>> keyboard.
>>>>
>>>>> Even better is to specify the version of Python this program is
>>>>> presumably written in, and what Linux distro. Then you say it's a GUI
>>>>> program, so you should specify which GUI library you're using.
>>>>
>>>> Python 2.6, Ubuntu 9.10, tkinter
>>>>
>>>>> Now if I do a bunch of guessing, I might come up with the likelihood
>>>>> that your "Nautilus" is supplying a different current directory
>>>>> than the
>>>>> one the script is located in. You can find that out by looking at:
>>>>> os.path.abspath(os.curdir)
>>>>
>>>> OK, thanks. If that is the case how do I correct it?
>>>>
>>>>> But of course how you print that depends on what GUI package you're
>>>>> running.
>>>>>
>>>>> DaveA
>>>>>
>>>>
>>>> Regards, Jim
>>>>
>>>>
>>> You forgot to include the list in your reply. Try using reply-all
>>> instead.
>>>
>>> If you determine that the current directory is your problem, and that
>>> Nautilus isn't setting it the way you'd like, then you may have to
>>> resort to other ways to identify the other files you mention. Easiest
>>> way might be to use the __file__ attribute of each module, which gives
>>> its complete path. So your code in the main script could do something
>>> like (untested):
>>> target = os.path.dirname(__file__)
>>> os.chdir(target)
>>
>> I added target = os.path.dirname(__file__) and printed the result. If
>> I run the script in Idle it produces the correct result and shows the
>> path as /usr/bin. Running it form the terminal produces the correct
>> result but shows the path empty. Running it from Nautilus or a
>> launcher does not produce the correct result and shows the full path
>> to the directory that contains the script and its files.
>>
>> If I add os.chdir(target) to the script, Idle and the terminal stop
>> producing the correct result and the launcher and Nautilius start
>> producing the correct result. This is exactly the opposite of what I
>> expected.
>>
>>> Better is usually to ignore current directory, and passed the desired
>>> directory name into whatever function is going to use it.
>>
>> I'll keep this in mind for anything I write. Right now I'm trying to
>> understand some examples that were produced in a Windows environment.
>> Like I said at the top I am new to Linux (Ubuntu) and don't fully
>> understand its requirements.
>>
>>> HTH,
>>>
>>> DaveA
>>>
>>>
>> Thanks, Jim
>>
> Apparently in Linux, the __file__ contains a relative path. So you
> should normalize it, the easiest way being
>
> target = os.path.abspath(os.path.dirname(__file__))
>
> This should then be the same no matter how you launch the script.
>
> Any other guesses would need some sample code that shows the failure.
> But you probably need to simplify it substantially to convince a
> volunteer to look at it.
>
> DaveA
>
>
Using your suggestion now allows me to run the program from anywhere 
except Idle, which gives file permission and database errors.  I didn't 
paste the error here because my intent was to figure out how to run 
python programs outside of Idle and you have shown me how to do it.  If 
someone is interested in seeing the error message I will certainly post it.

Just want to say thanks for furthering my python understanding.

Regards  Jim



More information about the Python-list mailing list