Python ImportError: No module named 'requests' after confirming install

佐手、 提交于 2019-11-28 12:28:56

问题


I'm new to python and set up a few things in terminal and am now trying to run python code in Atom.

My first three lines in my code editor are:

import re
import requests
import robobrowser

When I run the code I keep getting the

ImportError: No module named robobrowser

I'm assuming it has to do with the path or placement of my files, but I'm not sure how to check that.

Versions I'm running in terminal:

(prot) MacBook:prot myname$ python --version
Python 3.7.3
(prot) MacBook:prot myname$ pip --version
pip 19.0.3 from /Users/myname/prot/prot/lib/python3.7/site- 
packages/pip (python 3.7)

I also created a virtual env using the following:

MacBook:prot myname$ python3 -m venv prot
MacBook:prot myname$ source prot/bin/activate

So my default command line displays:

(prot) MacBook:prot myname$

Running the following shows that the needed packages are there.

(prot) MacBook:prot myname$ pip3 list
Package        Version 
-------------- --------
beautifulsoup4 4.7.1   
certifi        2019.3.9
chardet        3.0.4   
idna           2.8     
pip            19.0.3  
requests       2.21.0  
robobrowser    0.5.3   
setuptools     40.8.0  
six            1.12.0  
soupsieve      1.9     
urllib3        1.24.1  
Werkzeug       0.15.1

Edit (updated based on comments):

import sys
print(sys.version)

in Atom outputs (used to say 2.#, so I updated the version in atom):

3.7.3 (default, Mar 27 2019, 09:23:15)

[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)]

Robobrowser shows up in terminal correctly:

(prot) MacBook:prot myname$ pip3 show robobrowser
Name: robobrowser
Version: 0.5.3
Summary: Your friendly neighborhood web scraper
Home-page: https://github.com/jmcarp/robobrowser
Author: Joshua Carp
Author-email: jm.carp@gmail.com
License: MIT
Location: /Users/myname/prot/prot/lib/python3.7/site-packages
Requires: beautifulsoup4, requests, six, Werkzeug
Required-by: 

To check the remaining robobrowser issue, I ran this in atom:

from pip import _internal
_internal.main(['list'])

and got this (notice robobrowser is missing):

Package         Version 
--------------- --------
cached-property 1.5.1   
certifi         2019.3.9
chardet         3.0.4   
idna            2.8     
pip             19.0.3   
python-dateutil 2.8.0   
requests        2.21.0  
setuptools      40.8.0  
six             1.12.0  
urllib3         1.24.1  
wheel           0.33.1  

In terminal, running

pip show requests
pip show robobrowser

returns the same Location

/Users/myname/prot/prot/lib/python3.7/site-packages

In atom, running

import sys
print(sys.path)

outputs

['', '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages']

So I think I need to add the above location to the sys.path


回答1:


Question: ImportError: No module named 'robobrowser'

Run the following, verify if a module can be found installed and the Location is in the sys.path.

import sys
import pkg_resources as pkg

sys_path = sys.path
ws_entries = pkg.WorkingSet().entries

print('Python    :{}'.format(sys.version.split('\n')[0]))

key = 'robobrowser'    

key_found = False
for dist in pkg.working_set:        
    if key in dist.key:
        key_found = True
        print('Package   :{}'.format(dist.project_name))
        print('Version   :{}'.format(dist.version))
        print('Location  :{}'.format(dist.location))

        importable = False
        for info in pkg.find_distributions(dist.location, only=True):
            if key in info.key:
                importable = True
                break

        if importable:
            print('Importable: Location were on sys.path')
        else:
            print('Not importable: Unless {} were added to sys.path.'.format(dist.location))

        print('sys.path  :{}'.format(dist.location in sys_path))
        print('WorkingSet.entries:{}'.format(dist.location in ws_entries))
else:
    if not key_found:
        print("No package like '{}' found.".format(key))

Output: Example key = 'requests'

Python    : 3.5.3 (default, Jan 19 2017, 14:11:04) 
Package   : requests
Version   : 2.19.1
Location  : /usr/local/lib/python3.5/dist-packages
Importable: Location were on sys.path
sys.path  : True
WorkingSet.entries: True



回答2:


I solved the problem although not sure if I did so the proper way. I exited the venv and went back to my main directory. Installed robobrowser then went back into the specific venv/directory I was working out of. This solved my issue.



来源:https://stackoverflow.com/questions/55404894/python-importerror-no-module-named-requests-after-confirming-install

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!