Installing Python Libraries

DanielB

New Member
Messages
9
Reaction score
0
Points
0
Where do I copy new python libraries to, or how do I go about installing them, so that I can use them in scripts.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.
 

DanielB

New Member
Messages
9
Reaction score
0
Points
0
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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.
 

DanielB

New Member
Messages
9
Reaction score
0
Points
0
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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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?

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.
 

DanielB

New Member
Messages
9
Reaction score
0
Points
0
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:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:
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.
 

DanielB

New Member
Messages
9
Reaction score
0
Points
0
*embarrassed* Just realised that this has worked the whole time, But my lib had an error in it

Thanks for all the help!
 
Top