user2105469
user2105469

Reputation: 1433

PYTHONPATH permissions

I've copied a few modules from the standard site-packages location on my windows pc into an EC2 ubuntu instance's user's home directory: /home/theuser/data/projects/mypack.

mypack contains

1) one empty __init__.py and

2) one subdirectory mymodules, in which I have my python module files along with one __init__.py that runs the from thefile import thefile statements.

I've made sure to edit both .bashrc and .profile for theuser in order to update PYTHONPATH. And I made sure to start a fresh session, logged in as theuser.

import mypack runs fine if I'm in /home/theuser/data/projects. Anywhere else, say in theuser's home directory, I get

>>> import mypack
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
 ImportError: No module named mypack

I'm not that familiar with unix permissions, and the setup above tested OK on my pc. Is some unix permission detail tripping me up?

PYTHONPATH does seems to register the change properly:

>>> import sys
>>> print sys.path
['', 
'/usr/local/lib/python2.7/dist-packages/distribute-0.6.48-py2.7.egg', 
'/home/theuser', 
'/home/theuser/data/projects/mypack', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-linux2', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages']

Thanks.

theuser@ip-12-345-67-8:~/data/projects$ ls -lah
total 20K
drwxrwxr-x 5 theuser theuser 4.0K Jul  2 18:50 .
drwxr-xr-x 5 theuser theuser 4.0K Jul  2 16:48 ..
drwxrwxr-x 2 theuser theuser 4.0K Jul  2 17:18 database
drwxrwxr-x 2 theuser theuser 4.0K Jul  2 18:28 analysis
drwxrwxr-x 5 theuser theuser 4.0K Jul  2 19:18 mypack

Upvotes: 0

Views: 685

Answers (2)

Mattie B
Mattie B

Reputation: 21279

PYTHONPATH should contain /home/theuser/data/projects, not /home/theuser/data/projects/mypack.

When you ask Python to import mypack, it looks for either mypack.py or a directory mypack containing __init__.py using its search path.

This works when you're inside .../projects, because mypack/__init__.py is available there (and the '' entry in sys.path searches the current working directory), but when you're not, it cannot find mypack/__init__.py with the PYTHONPATH you've supplied.

Upvotes: 2

rread
rread

Reputation: 159

It looks like you need to have "/home/theuser/data/projects" in your PYTHONPATH, instead of "/home/theuser/data/projects/mypack".

Upvotes: 1

Related Questions