Search code examples
pythonimporterror

Python script run on command line can't find site packages


I've written a Python application with Python 2.7 using PyDev for Eclipe, where the script executes perfectly. However, if I try to run it on the command line, I get the following error:

importError: No module named openpyxl

openpyxl is one of the external libraries I use in my application. I searched for solutions and found multiple threads, some of which on this site. All of them handled modules that were created by the programmer himself and requires appending the src folder to the path. This doesn't work for me. I also tried adding the site package directory to my Python Path, but still the error remains.

Also, the script that is run (main.py) is located in the src directory, like so:

/Crawler/
    /src
        __init__.py
        main.py
        /Crawlers
            __init__.py
            Crawler1.py
            Crawler2.py

How do I configure my script so it finds all external libraries when executed from the command line?


Solution

  • Check whether the default python version is actually 2.7. Run python --version. If the default is not 2.7, call the script with the 2.7 interpreter;

    python2.7 <yourscriptname>
    

    Or if you are on UNIX or OSX, change the first line of the script to actually call python 2.7;

    #!/usr/bin/env python2.7
    

    Edit: If the python version is correct, check that you can import the module from the interactive interpreter. If you get output similar to the one below, openpyxl isn't installed correctly.

    python
    Python 2.7.8 (default, Jul 20 2014, 12:09:51) 
    [GCC 4.2.1 Compatible FreeBSD Clang 3.4 (tags/RELEASE_34/final 197956)] on freebsd10
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import openpyxl
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: No module named openpyxl
    >>>