0

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

Community
  • 1
  • 1
hansence
  • 11
  • 4
  • What command did you use to create your virtualenv? Did you explicitly use `-p python3`? Are you sure the python interpreter inside the virtualenv is pointing to the correct one? Can you import them separately (e.g. from the IDLE interpreter)? What command are you using to launch your script? – code_dredd Mar 28 '19 at 19:01
  • I edited my post to answer your first question. I'm not sure how to tell if the interpreter is pointing to the correct thing or how to use IDLE. To run the script, I downloaded a package in Atom simply called Script and run it in there and not with terminal. – hansence Mar 28 '19 at 19:10
  • update: I configured Script in Atom to use python3 and I got past the 'import requests' line in my code. Still have the issue with 'import robobrowser'. – hansence Mar 28 '19 at 19:23
  • @hansence: Try `from robobrowser import RoboBrowser` – stovfl Mar 28 '19 at 19:26
  • Tried that, got a ModuleNotFoundError: No module named 'robobrowser' error – hansence Mar 28 '19 at 19:26
  • Edited. Trying to research other threads as well for this issue. – hansence Mar 28 '19 at 20:01

2 Answers2

0

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
stovfl
  • 14,998
  • 7
  • 24
  • 51
  • @hansence: Yes, and `beautifulsoup4/Werkzeug` are also missing. Compare the `Location` of `pip show requests` with the one from `pip show robobrowser`. Then verify if this `Location` is in `sys.path`. If not add it using `sys.path.append(...` – stovfl Mar 29 '19 at 16:06
  • The location for requests and robobrowser is the same when I checked in terminal. I don't see that location in sys.path so I'm trying to use that append command. – hansence Mar 29 '19 at 17:16
  • Relevant [different-list-of-installed-packages-sing-pip-list-and-pip-get-installed-dist](https://stackoverflow.com/a/31544858/7414759) – stovfl Mar 29 '19 at 17:24
  • I checked out that thread. Running "pip3 list --local" returns the same list as "pip3 list", but items are missing when I run the "internal list" code in atom. I was just thinking that if this was the issue, then importing requests would be throwing an error but it isn't anymore. I think robobrowser is in the wrong spot but I installed it after setting up my venv.... I could try a simple uninstall and reinstall - that didn't work either – hansence Mar 29 '19 at 17:35
  • @hansence: Remove the output, is not usefull at all. It seems even not `dists = [d for d in pkg.working_set]` completes. Should be: `sys.path.append('/Users/myname/prot/prot/lib/python3.7/site-packages')` – stovfl Mar 29 '19 at 20:55
0

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.

hansence
  • 11
  • 4
  • Does the output of `pip3 show robobrowser` change, different `Location`? – stovfl Apr 01 '19 at 17:08
  • I had the same location output of `/Users/myname/prot/prot/lib/python3.7/site-packages` – hansence Apr 01 '19 at 18:19
  • Does the `print(sys.path)` or `_internal.main(['list'])` change? – stovfl Apr 01 '19 at 18:23
  • I think robobrowser did show up in the list when I ran the latter in atom afterwards. I worked for a few hours on it yesterday and at one point I had new errors, so I backed out of the prot venv and started over with a new venv so I would have a blank slate. I installed everything again and cloned a zip from github, where before I was copying files one-by-one. My file structure was different with my second venv and things ran a lot smoother. I think my python files were one level too high with my first attempt. – hansence Apr 01 '19 at 22:22