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).
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.
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.
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.
EDIT v2 -
EDIT v3: Still having trouble getting GoogleMaps project to work
EDIT v4: Started from scratch with a venv following directions from http://docs.python-guide.org/en/latest/dev/virtualenvs/
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
>>>
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
:
virtualenv env
. env/bin/activate
. env/bin/activate
. That will switch you from your global python
to the one installed in env/
pip install
dependencies deactivate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.