packt-cli / Packt-Publishing-Free-Learning

Scripts that automatically claim and download free daily eBooks from https://www.packtpub.com/packt/offers/free-learning

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"ImportError: No module named configparser" error on Python 2.7.13

develsites opened this issue · comments

I've added one task to launchd on Mac and during executing it I have an error which provided below:

Traceback (most recent call last):
  File "/Users/develsites/packt_grabber/src/packtPublishingFreeEbook.py", line 10, in <module>
    import configparser
ImportError: No module named configparser

If I run the script inside in the src folder directly in terminal then it works correctly.

How to solve this issue? Thanks

My first guess is that it's because such package does not exist in Python 2.7 - we can either import if from six or import it like below. I'll make a pull request at most that evening and add you as a reviewer.

if sys.version_info[0] == 2:
    import ConfigParser as configparser
else:
    import configparser

Have you installed the dependencies in virtualenv? Are you using virtualenv? I made a new Python 2.7.13 virtualenv, installed all dependencies and everything works fine (I can download books).

For me it seems you haven't installed dependencies (can you confirm or deny it?), but importing it from six may be a good idea anyway.

@develsites Pull request on it's way #71.

Thank @mjenczmyk for replaying. So, I've tested your solutions but I've got another one

Traceback (most recent call last):
  File "/Users/develsites/packt_grabber/src/packtPublishingFreeEbook.py", line 12, in <module>
    import requests
ImportError: No module named requests

For some reasons my Python doesn't see my dependencies when it was run by using crontab or launchd on Mac.

I executed this script through shell scripting (I made the checking a date) directly without any virtual environments. The Python was installed by using Homebrew. Please look at an additional information below, maybe it'll be useful:

MacBook-Pro:scripts develsites$ which python
/usr/local/bin/python
MacBook-Pro:scripts develsites$ /usr/local/bin/python --version
Python 2.7.13
MacBook-Pro:scripts develsites$ ls -al /usr/local/bin/python
lrwxr-xr-x  1 develsites  admin  34 Jul  5 17:33 /usr/local/bin/python -> ../Cellar/python/2.7.13/bin/python

I have a question: could I check installed dependencies for current Python version?

Thanks so much for help;)

@develsites You do not use virtualenv for cron, because you're using system-wide "python" file, most propably.

I suggest write which python, then use absolute path of python binary in $PYTHON -m pip install requests and in cron-tab rules.

I also thought it was worthwhile to verify that you are not modifying the PYTHONPATH environment variable in an undesirable way.

@develsites What package manager for Python packages are you using? Is it pip or some other OS relates stuff? That question may sound silly, but I will ask it anyway: are you sure you have installed script dependencies (ex. by running pip install -r requirements.txt)?

If you are using pip you can see installed packages by running pip freeze.

Thank you @ad-m and @mjenczmyk for helping:) Unfortunately, I couldn't fix this yet. I use Python directly without virtualenv. So, the absolute path which was installed by Homebrew is /usr/local/Cellar/python/2.7.13_1/bin/python2 so, when I use /usr/local/Cellar/python/2.7.13_1/bin/python2 -m pip install requests or /usr/local/Cellar/python/2.7.13_1/bin/python2 -m pip install configparser I have:

MacBook-Pro:scripts develsites$ /usr/local/Cellar/python/2.7.13_1/bin/python2 -m pip install requests
Requirement already satisfied: requests in /usr/local/lib/python2.7/site-packages
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/site-packages (from requests)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/site-packages (from requests)
Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python2.7/site-packages (from requests)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python2.7/site-packages (from requests)
MacBook-Pro:scripts develsites$ /usr/local/Cellar/python/2.7.13_1/bin/python2 -m pip install configparser
Requirement already satisfied: configparser in /usr/local/lib/python2.7/site-packages
MacBook-Pro:scripts develsites$ /usr/local/Cellar/python/2.7.13_1/bin/python2 -m pip install requests
Requirement already satisfied: requests in /usr/local/lib/python2.7/site-packages
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python2.7/site-packages (from requests)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python2.7/site-packages (from requests)
Requirement already satisfied: idna<2.7,>=2.5 in /usr/local/lib/python2.7/site-packages (from requests)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/local/lib/python2.7/site-packages (from requests)

Regarding the PYTHONPATH variable so, I didn't find any, probably, it's missing.

Yes, @mjenczmyk I installed all packages by using pip pip install -r requirements.txt So, when I checked them on /usr/local/lib/python2.7/site-packages so, they exist:

MacBook-Pro:scripts develsites$ which pip 
/usr/local/bin/pip

MacBook-Pro:scripts develsites$ ls -al /usr/local/bin/pip
lrwxr-xr-x  1 developer  admin  33 Sep  4 10:42 /usr/local/bin/pip -> ../Cellar/python/2.7.13_1/bin/pip

MacBook-Pro:scripts develsites$ pip --version
pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)

MacBook-Pro:scripts develsites$ pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
beautifulsoup4 (4.5.1)
certifi (2017.7.27.1)
chardet (3.0.4)
configparser (3.5.0)
future (0.16.0)
idna (2.6)
pip (9.0.1)
requests (2.18.4)
setuptools (32.1.0)
urllib3 (1.22)
wheel (0.24.0)

Thanks

Thank everybody for helping ;)
The issue has been solved. The reason was different paths to the Python binary (one was system-wide installed and one installed from brew). Because I use some shell script which is using different binaries.

Thanks so much for everybody :)