Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting python path

I have a Django app and I'm getting an error whenever I try to run my code:

Error: No module named django_openid

Let me step back a bit and tell you how I came about this:

  1. I formatted my computer and completely re-installed everything -- including virtualenv, and all dependent packages (in addition to Django) required for my project based on settings in my requirements.txt folder
  2. I tried doing python manage.py syncdb and got the error

I googled the issue, and many people say it could be a path problem.

I'm confused as to how I go about changing the path variables though, and what exactly they mean. I found some documentation, but being somewhat of a hack-ish noob, it kind of goes over my head.

So my questions are:

  1. What exactly is their purpose -- and are they on a system based level based on the version of Python or are they project dependent?
  2. How can I see what mine are set to currently?
  3. How can I change them (ie. where is this .profile file they talk of and can I just use a text editor)

Any input you would have would be great as this one is stumping me and I just want to get back to writing code :-)

like image 915
user1328021 Avatar asked Feb 20 '23 06:02

user1328021


2 Answers

The path is just the locations in your filesystem in which python will search for the modules you are trying to import. For example, when you run import somemodule, Python will perform a search for somemodule in all the locations contained in the path (sys.path variable).

You should check the path attribute in sys module:

import sys
print sys.path

It is just a regular list, sou you could append/remove elements from it:

sys.path.append('/path/to/some/module/folder/')

If you want to change your path for every python session you start, you should create a file to be loaded at startup, doing so:

  1. Create a PYTHONSTARTUP environment variable and setting it to your startup file. E.g.: PYTHONSTARTUP=/home/user/.pythonrc (in a unix shell);
  2. Edit the startup file so it contains the commands you want to be auto-executed when python is loaded;

An example of a .pythonrc could be:

import sys
sys.path.append('/path/to/some/folder/')
like image 172
Valdir Stumm Junior Avatar answered Feb 22 '23 04:02

Valdir Stumm Junior


Do you really need to alter the path? It's always best to actually think about your reasons first. If you're only going to be running a single application on the server or you just don't care about polluting the system packages directory with potentially unnecessary packages, then put everything in the main system site-packages or dist-packages directory. Otherwise, use virtualenv.

The system-level package directory is always on the path. Virtualenv will add its site-packages directory to the path when activated, and Django will add the project directory to the path when activated. There shouldn't be a need to add anything else to the path, and really it's something you should never really have to worry about in practice.

like image 42
Chris Pratt Avatar answered Feb 22 '23 05:02

Chris Pratt