+ Reply to Thread
Results 1 to 9 of 9

Thread: Installing Python Libraries

  1. #1
    DanielB's Avatar
    DanielB is offline x10Hosting Member DanielB is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    9

    Installing Python Libraries

    Where do I copy new python libraries to, or how do I go about installing them, so that I can use them in scripts.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Installing Python Libraries

    Examine sys.path for the current library search path. Append to it to add your own library directory, if need be:
    Code:
    import sys, os
    homelibdir = os.path.join(os.path.expanduser('~'), 'lib', 'python')
    if homelibdir not in sys.path:
        sys.path.append(homelibdir)
    I believe using '/' as a path separator is supported on the major platforms, but "os.path.join" is guaranteed to produce a properly formatted pth on all platforms.

    I haven't tested it, but try adding the above code to ~/.pythonrc so that you don't have to do it for every python script.

  3. #3
    DanielB's Avatar
    DanielB is offline x10Hosting Member DanielB is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    9

    Re: Installing Python Libraries

    The path it ended up with was

    /home/danielb/lib/python

    I cant seem to find this directory. Where am I putting these files?

    (also, on a side note, I was able to get it to tell me where the os lib was installed [/usr/lib/python2.4/], if that helps)
    Last edited by DanielB; 12-17-2008 at 09:17 PM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Installing Python Libraries

    Quote Originally Posted by DanielB View Post
    The path it ended up with was

    /home/danielb/lib/python

    I cant seem to find this directory. Where am I putting these files?

    [...]
    Chances are you'll need to create it. By default, there is no folder for python libraries. As you saw, the default search directories are "." (which doesn't help with libraries) and "/usr/lib/python2.4" (which you don't have write access to). You can use the cPanel file manager or your FTP application of choice to create /home/danielb/lib/python; you'll also probably have to create /home/danielb/lib. Here's one page with more info on using cPanel's file manager. I'm linking to this page not because it's a great tutorial but because it's the first that turned up in a google search; if it doesn't have enough info, check some of the other results. Make sure you create lib/python in your home folder, not web root for your site.

  5. #5
    DanielB's Avatar
    DanielB is offline x10Hosting Member DanielB is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    9

    Re: Installing Python Libraries

    I still cant get it to work. >_<

    Any Chance you can make it create a dummy file I can look for, or I can give you a r/o account for my FTP so you can see whats wrong?

    tried using
    Code:
    #!/usr/bin/python
    
    import os
    
    hlb = os.path.expanduser('~')
    os.mkdir(hlb+'/lib')
    os.mkdir(hlb+'/lib/python')
    file(hlb+'/lib/python/FINDME.txt').close()
    but its getting nowhere....
    Last edited by DanielB; 12-23-2008 at 05:36 AM. Reason: used syntax tag, not code

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Installing Python Libraries

    When asking for help, remember to always say what you expected to happen and what actually happens (i.e. what should be right and what's going wrong). Are you having trouble creating the folders? Are you having problems loading the libraries? Are you having difficulty setting the library path?

    Quote Originally Posted by DanielB View Post
    I still cant get it to work. >_<

    [...]

    tried using
    Code:
    #!/usr/bin/python
    
    import os
    
    hlb = os.path.expanduser('~')
    os.mkdir(hlb+'/lib')
    os.mkdir(hlb+'/lib/python')
    file(hlb+'/lib/python/FINDME.txt').close()
    A few notes on the code:
    • Use os.makedirs rather than calling mkdir multiple times.
    • Wrap a "try/except OSError" around os.mkdir or os.makedirs call in case directory exists.
    • Use open rather than file to create files. You won't be able to use file() in Python 3 and, according to the Python 2 docs, "When opening a file, it’s preferable to use open() instead of invoking the file constructor directly."
    • Include a mode when opening a file. The default is 'r' (read), which will cause your code to fail (opening a non-existent file in read mode will throw an error). Even if you want to open a file in the default mode, it's best to be explicit; it's less error prone and more readable.

    Using a file transfer app/file manager is better than Python for creating folders as it's easier to check ownership, permissions and even success. The above code can fail even if the folders are created, giving you a false negative.

  7. #7
    DanielB's Avatar
    DanielB is offline x10Hosting Member DanielB is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    9

    Re: Installing Python Libraries

    I modified the script, and it has created the file. However, when I put the library there i still cant import it.

    it made the file in ./lib/python
    Last edited by DanielB; 12-28-2008 at 10:44 PM.

  8. #8
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Installing Python Libraries

    Quote Originally Posted by DanielB View Post
    I modified the script, and it has created the file. However, when I put the library there i still cant import it.

    it made the file in ./lib/python
    That's why I wrote:
    Quote Originally Posted by misson View Post
    Using a file transfer app/file manager is better than Python for creating folders as it's easier to check ownership, permissions and even success.
    You shouldn't create folders within Python when installing a module, unless you're writing an installer for the module. Just create the folders and install the module yourself. It's much less of a headache because you know the module is installed in the correct place.

  9. #9
    DanielB's Avatar
    DanielB is offline x10Hosting Member DanielB is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    9

    Re: Installing Python Libraries

    *embarrassed* Just realised that this has worked the whole time, But my lib had an error in it

    Thanks for all the help!

+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 07-10-2009, 03:30 AM
  2. Python, SQlite and Django?
    By phineas in forum Free Hosting
    Replies: 2
    Last Post: 08-17-2008, 02:02 AM
  3. Python / MySQLdb
    By Korexio in forum Free Hosting
    Replies: 9
    Last Post: 11-29-2007, 08:29 AM
  4. Python/.htaccess help
    By *Face in forum Free Hosting
    Replies: 6
    Last Post: 09-21-2007, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers