Home:ALL Converter>$PYTHONPATH not working on OSX

$PYTHONPATH not working on OSX

Ask Time:2016-03-03T05:47:07         Author:gamda

Json Formatter

I have been at this for a day and have found no solution. My python project is very levels deep and some of the modules have to be run independently (no -m flag, no relative imports). For this reason we decided to add the root folder to the PYTHONPATH. I am running OSX 10.11.3. Everything had been running smoothly until yesterday. I could run independent modules from the terminal with no issues in both Python 2.7 and 3.5. Yesterday, without modifying my PYTHONPATH or any other environment setting, running any of these independent modules from the terminal now gives me import errors in both Python 2 and 3.

Here is my working tree:

/Users/sintrafico/Documents/code/central_maestra
- reports_server.py
- api_server.py
- sintrafico
    - sql
        - SQLConnection
    - api
        - incident
            - csv (not package)
                - independent_module
            - tests

I was running my tests yesterday with coverage with no problem, but now I can't.

Python 2 python csv/independent_module.py gives error:

Traceback (most recent call last):
  File "csv/independent_module.py", line 13, in <module>
    from sintrafico.sql import SQLConnection
ImportError: No module named sintrafico.sql

Python 3 python3 csv/independent_module.py gives error:

Traceback (most recent call last):
  File "csv/create_bemobile_csv.py", line 13, in <module>
    from sintrafico.sql import SQLConnection
ImportError: No module named 'sintrafico'

From within the incident folder, yesterday this command was working fine: coverage run -m unittest discover. Now all the tests fail because ImportError: No module named 'reports_server'.

Since that started happening yesterday, I have been messing with my PYTHONPATH. I left it blank and it didn't work (as expected), I have added the path now several ways:

  • export PYTHONPATH=“${PYTHONPATH}:/Users/sintrafico/Documents/code/central_maestra"
  • export PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}"
  • export PYTHONPATH=“${PYTHONPATH}:/Users/sintrafico/Documents/code/central_maestra/"
  • export PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}"

And also setting it without appending the PYTHONPATH since it was empty to begin with. I restarted my computer several times with no success. Also note, I have a .bash_profile and .bashrc with the following contents:

# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}“
export PYTHONPATH

And lastly, what bugs me the most is that if I run the tests inside PyCharm they all run and pass, I get no import errors at all.

Author:gamda,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35758743/pythonpath-not-working-on-osx
yy