1

I am setting PYTHONPATH to have a directory that includes a few .py files.

When I go into python and type "import file", the file cannot be find (it says "No module named wsj10").

If, however, I cd to the directory, and repeat the same process, then the file is found.

I am just not sure why PYTHONPATH is being ignored. I followed exact instructions from installation instructions of some software, so I know I am doing the right thing.

Any circumstances under which PYTHONPATH will be ignored, or import won't work?

Thanks.

Following a comment below, here is a transcript:

  1. untar file1.tgz to file1/. file1.tgz contains a library/file called file1.py.

  2. type in the shell:

    export PYTHONPATH=`pwd`/file1/:./
    
  3. echo $PYTHONPATH shows the variable was set.

  4. run python and type "import file1"

I get the error:

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

If I do first "cd file1" and then to import file1 it identifies the file.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
kloop
  • 4,537
  • 13
  • 42
  • 66

1 Answers1

1

Any circumstances under which PYTHONPATH will be ignored, or import won't work?

Yes. I've set PYTHONPATH in my /home/me/.bashrc and all worked ok from terminal, but when Apache w/ mod_wsgi starts my python scripts, it acts under sysem or dedicated user, which knows nothing of my .bashrc.

For this particular situation, I just used apache config to set python path for apache (WSGIPythonPath option).

sdd
  • 721
  • 9
  • 23
  • Hi, I have the same issue right now but I don't know apache very well. Can you tell me how you set the python path for apache? – Rahul Jul 27 '17 at 19:27
  • 1
    Apache probably runs as a difference user so it will not source your `.bashrc`. This means that `PYTHONPATH` *is not set* -- that is not the same thing as *it is ignored* . – ingomueller.net Aug 26 '19 at 15:05