Results 1 to 9 of 9

Thread: Running Python Programs from Arbitrary Directory

  1. #1
    Join Date
    Aug 2008
    Beans
    12

    Arrow Running Python Programs from Arbitrary Directory

    I am just getting situated in Ubuntu, and I am having trouble running python programs. I've used python on other systems before.
    The trouble is, when I try to run a python program from the terminal, it ends up like this:
    Code:
    $ python helloworld.py
    python: can't open file 'helloworld.py': [Errno 2] No such file or directory
    That is, of course, when I have helloworld.py stored somewhere besides my user folder. It works in my user folder, but I would rather not keep python programs there. It also works when I change the terminal directory:
    Code:
    cd ~/Desktop/Folder
    python helloworld.py
    Hello, World!
    But is there a way to conveniently run python programs from a separate folder, without having to change the terminal directory?

    FYI, I have tried sys.path.append(), PATH=$PATH:, and yes, the first line of code is #!/usr/bin/python

    Thanks,
    Umenzi

  2. #2
    Join Date
    Jan 2008
    Location
    Auckland, New Zealand
    Beans
    3,133
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Running Python Programs from Arbitrary Directory

    You can put them in ~/bin or in /usr/bin or you can modify your path. Not your python path but your bash path. In a terminal enter:
    Code:
    PATH=$PATH:/home/your_user_name/Desktop/Folder
    I just put all my scripts in ~/bin which should already be in your path.
    Last edited by ad_267; August 11th, 2008 at 09:52 AM.

  3. #3
    Join Date
    May 2007
    Beans
    1,091

    Re: Running Python Programs from Arbitrary Directory

    also you can type in a terminal
    Code:
    echo $PATH
    and it will give you all of the directories that it will check for your hello world file. if you move it to one of the directories that is displayed it will work.

  4. #4
    Join Date
    Aug 2008
    Beans
    12

    Re: Running Python Programs from Arbitrary Directory

    PATH=$PATH

    Doesn't seem to work, nor does copying my python file into one of the directories already listed in "echo $PATH". Also, the new directory will show up in echo, but it won't find files from there. I'm confused, by for now I guess I'll just change my terminal directory.

  5. #5
    Join Date
    Jan 2008
    Location
    Auckland, New Zealand
    Beans
    3,133
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Running Python Programs from Arbitrary Directory

    Ahh I think I get what you're doing wrong now.

    You need to put as the very first line in your python file:
    Code:
    #!/usr/bin/env python
    And then don't run the script with "python helloworld.py", just use "helloworld.py"

  6. #6
    Join Date
    Jun 2006
    Location
    $ pwd _
    Beans
    3,999
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Running Python Programs from Arbitrary Directory

    ... and make sure helloworld.py is executable.
    Code:
    chmod u+x helloworld.py

  7. #7
    Join Date
    May 2009
    Beans
    11

    Re: Running Python Programs from Arbitrary Directory

    I was having the same problem as the above users, and not saving it to the desktop seemed to work, except for the fact that I am greeted with a new error message that reads as follows:

    Code:
    python helloworld.py
      File "helloworld.py", line 1
        Python 2.5.4 (r254:67916, Apr  4 2009, 17:55:16) 
                 ^
    SyntaxError: invalid syntax
    I know I am running 2.6.2, So what does this even mean?

  8. #8
    Join Date
    Jan 2008
    Location
    Auckland, New Zealand
    Beans
    3,133
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Running Python Programs from Arbitrary Directory

    You probably should have started a new thread rather than resurrecting this old one. What does the first line of your file look like? It shouldn't say "Python 2.5.4" so delete that if it does. It should only say "#!/usr/bin/env python"

  9. #9
    Join Date
    Apr 2009
    Location
    Dhaka,Bangladesh
    Beans
    1,010

    Re: Running Python Programs from Arbitrary Directory

    Quote Originally Posted by Umenzi View Post
    I am just getting situated in Ubuntu, and I am having trouble running python programs. I've used python on other systems before.
    The trouble is, when I try to run a python program from the terminal, it ends up like this:
    Code:
    $ python helloworld.py
    python: can't open file 'helloworld.py': [Errno 2] No such file or directory
    That is, of course, when I have helloworld.py stored somewhere besides my user folder. It works in my user folder, but I would rather not keep python programs there. It also works when I change the terminal directory:
    Code:
    cd ~/Desktop/Folder
    python helloworld.py
    Hello, World!
    But is there a way to conveniently run python programs from a separate folder, without having to change the terminal directory?

    FYI, I have tried sys.path.append(), PATH=$PATH:, and yes, the first line of code is #!/usr/bin/python

    Thanks,
    Umenzi
    Can you post your complete code?

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •