0

I'm having a tough time finding a website that clearly explains how to import python modules without errors. I have a file called coffee_shop.py in a directory called ~/Desktop/programming_feb_23_v.

When I tried to run it, I would get the python import error from the googlemaps package. I downloaded the package directly "googlemaps-2.1.1.tar.gz" - I unzipped it and put the googlemaps dir directly in the same directory as the scrape_google.py program (as well as all the other directories that came as child directories of googlemaps-2.1.1).

enter image description here

When I tried re-running python, I got "requests" module not found error. I then tried downloading the requests module and put that in the same folder as __init__.py for googlemaps. It still seems to think I don't have the request file, even though I have that directory and it has an __init__.py file in it, I still have a problem when trying to run the file.

enter image description here

Is there a smarter way to do this? I was reading about Python paths, but still not entirely sure what that means. For reference, I have printed my sys.path. I'm wondering if there is a way I can just put all my python packages in one place (a lib?) and not have to manually download packages every time I want to run them.

enter image description here

Actionable Questions: - How do I set up my imports so I can (1) run this program and (2) not have to manually download them every time?

Other Notes = I'm running Python 2.7

EDIT - tried installing a virtual env and still couldn't get the program to run, even though it says it's installed.

enter image description here

EDIT v2 -

enter image description here

enter image description here

EDIT v3: Still having trouble getting GoogleMaps project to work

enter image description here

EDIT v4: Started from scratch with a venv following directions from http://docs.python-guide.org/en/latest/dev/virtualenvs/

enter image description here

stk1234
  • 1,036
  • 3
  • 12
  • 29
  • 3
    What's wrong with using pip? – Ignacio Vazquez-Abrams Feb 24 '15 at 03:29
  • 1
    Please don't post images of text: just paste the text itself, inside a code or quote block. Did you run the [setup.py scripts](https://docs.python.org/2/install/) that came with those packages? The smarter way to do all this is to use [pip](https://pypi.python.org/pypi/pip). – PM 2Ring Feb 24 '15 at 03:43
  • Did this fix your problem? If so, don't forget to accept one of the answers so this question doesn't remain "unanswered". – boatcoder Feb 26 '15 at 23:18

2 Answers2

1

I'm going to recommend that you definitely get virtualenv and use it on every project. On ubuntu you can easily load this with

sudo apt-get install python-virtualenv

This will unfortunately give you a version that is slightly out of date. To remedy that, you just need to upgrade it.

sudo pip install --update virtualenv pip setuptools

Once that is done, create yourself a folder for virtual environments. I like to keep mine in ~/Python, this allows me to use my workon alias to activate the virtual environment.

So lets walk thru the next steps after the ones above.

$ mkdir ~/Python

This creates you a place to store all your virtual environment. Then these are the commands you will run every time you start a new project

$ cd ~/Python
$ mkdir projectname
$ cd projectname
$ virtualenv .projectname

I like to put my virtualenvs in a hidden folder because I seldom interact with them directly. Now that you have a virtualenv, you need to activate it so you can use it. You will need to activate the virtualenv everytime you use it. (Notice how the prompt changes once activated)

$ . .projectname/bin/activate
(.projectname) $ |

Notice the syntax on the above command is a little strange. The . tells bash to read the following script as if it had been typed at the command prompt, something that is VERY important if you want the virtual environment to actually work.

Now, when you need a python package to be importable, you just do

(.projectname) $ pip install packagename

And in your code you can do

import packagename

without any problem. If you want a copy of the workon alias, let me know and I'll post it here.

I think you are trying to import the wrong thing. Here is what I get when I import googlemaps. Pay special attention to the case of googlemaps

$ .foo/bin/activate
$ . .foo/bin/activate
(.foo)/tmp$ pip install googlemaps
Collecting googlemaps
    Downloading googlemaps-2.1.1-py2-none-any.whl
Collecting requests (from googlemaps)
    Downloading requests-2.5.2-py2.py3-none-any.whl (474kB)
        100% |################################| 475kB 422kB/s 
Installing collected packages: requests, googlemaps


Successfully installed googlemaps-2.1.1 requests-2.5.2
(.foo)/tmp$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import googlemaps
>>> 
boatcoder
  • 17,525
  • 18
  • 114
  • 178
  • Hmm, after following your directions, I tried creating a new folder ~/Python. I think I successfully set up a virtual env and imported googlemaps. I am thankfully no longer getting the "cannot import requests" error, however, I am getting an error that says "Can't find GoogleMaps." Any idea why this might be? – stk1234 Feb 24 '15 at 03:59
  • (.googlemaps_project)edohring-mbp:googlemaps_project edohring$ from googlemaps import GoogleMaps from: can't read /var/mail/googlemaps – stk1234 Feb 24 '15 at 04:02
  • How did you install googlemaps? ```pip install googlemaps``` Did you install it while the venv was activated – boatcoder Feb 24 '15 at 04:26
0

It sounds like your global site-packages folder is not on your path. This answer can help you see where your site-packages are and you can see what's on your path by typing echo $PATH at a terminal prompt. If they're not on your path, you can solve this by doing some searching around for the $PATH variable and how to set/update it in .bashrc, .bash_profile, or .profile files in your user home directory.

A better way to do this is to use something like virtualenv so that you can manage your Python dependencies on a per-project basis, instead of wrestling with global dependency mismatches if say, you needed one version of requests for one project and another version for a separate project...that can get hairy if you try to manage global packages.

Quick primer (and not the only way to do it) after installing virtualenv:

  1. from your project's directory type virtualenv env
  2. . env/bin/activate. That will switch you from your global python to the one installed in env/
  3. pip install dependencies
  4. When you're done or want to switch to another project, just type deactivate
  5. Make sure you repeat number 2 when you come back to work on that project
Community
  • 1
  • 1
rpmartz
  • 3,759
  • 2
  • 26
  • 36
  • Great! I got the virtual env started. I used pip install for google maps and requests, but am still getting an error for importing (see picture I just attached) – stk1234 Feb 24 '15 at 03:47
  • (.googlemaps_project)edohring-mbp:googlemaps_project edohring$ from googlemaps import GoogleMaps from: can't read /var/mail/googlemaps – stk1234 Feb 24 '15 at 04:02