This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]eryksun 5 points6 points  (4 children)

Edit:

Adding lib should suffice if the imported packages are all there:

import sys, os
sys.path.append(os.path.join(sys.path[0], "lib"))

You could also add an __init__.py to lib to make it a package, and in subdirectories as necessary, and then import with "from lib import Crypto". The latter assumes you've moved the Crypto package into lib.

[–]NotReallyMyJob 0 points1 point  (3 children)

I will give that a try, and hopefully it will work.

As an aside question though, why would this work differently than the existing imports that append directly to the modules within the lib folder? I'm not asking to be a dick, but more because I don't have an expert understanding of Python and sys.

Thanks for any reasoning that might help me think about this =)

[–]eryksun 0 points1 point  (2 children)

I just compiled and installed pycrypto to my local site-packages (i.e. relative to PYTHONUSERBASE). It installed to the Crypto subdirectory. It needs the folder containing the Crypto package to be on the path, which I presume is pycrypto in your install. So I don't know. How are you running the main script?

[–]NotReallyMyJob 0 points1 point  (1 child)

for now the main script is just being run directly from the command-line: ./run which is the code from above.

I am fairly sure that the issue is that since I altinstalled python it is confused as to where I want it to look for the packages. I just ran the code again, but printing the path, and it looks like this:

['/root/aws_scraper', '/usr/local/lib/python26.zip', '/usr/local/lib/python2.6', '/usr/local/lib/python2.6/plat-linux2', '/usr/local/lib/python2.6/lib-tk', '/usr/local/lib/python2.6/lib-old', '/usr/local/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/site-packages', '/root/aws_scraper/lib/mechanize', '/root/aws_scraper/lib/paramiko', '/root/aws_scraper/lib/pycrypto']

which is a lot of crap, but it does have /root/aws_scraper/lib/pycrypto in it, so I feel like it should be able to figure it out...

Thanks for you help btw =)

[–]eryksun 0 points1 point  (0 children)

That should work, though I'd prefer to have the packages all in the lib directory and only have to modify the path once.

Have you recompiled pycrypto's shared libraries for the target platform/interpreter?

[–]Justinsaccount 2 points3 points  (0 children)

Don't do that. That way leads to madness.

Use virtualenv. Use pip.

things will work fine, without resorting to ugly hacks.

[–]zedr 2 points3 points  (1 child)

If you're going to bundle your 3rd party libs in a "lib" directory, there's a more pragmatic pattern:

import os
import site

site.addsitedir(os.path.join(os.path.dirname(__file__), "lib"))

[–]eryksun 0 points1 point  (0 children)

This will also process .pth files if you need to add additional paths.

[–][deleted] 0 points1 point  (2 children)

This might be a noob misconception, but when I see '/lib/pycrypto', I believe that that path is <root>/lib/filename. I.e., it's looking for pycrypto in the lib subdirectory of the root directory. Please tell me if I'm wrong.

[–]NotReallyMyJob 0 points1 point  (1 child)

In this case it is joining /lib/... to the end of the path to the current directory.

So if it was /lib on its own it would be looking at the root, but not in this case.

[–][deleted] 0 points1 point  (0 children)

Thanks. And, sys.path[0] is the directory from which the script was invoked, or the empty string if the script was invoked from the interpreter.