Forums

ImportError: No module named googleads, but the module is installed in my virtualenv

Hello everyone, I do not really understand why I get this error.

  1. My virtualenv is in the following directory: /home/giancampo/.virtualenvs/AdWordsAPI/

  2. The module is at the following path: /home/giancampo/.virtualenvs/AdWordsAPI/lib/python2.7/site-packages/googleads/ (I have installed it using "pip install googleads" in a bash console inside the virtualenv and everything went well)

  3. My script is in the following position: /home/giancampo/.virtualenvs/AdWordsAPI/{folder1}/{folder2}/{scriptName}.py

The first import in my script is "from googleads import adwords" and I get the error message "ImportError: No module named googleads".

How could I check what is the problem behind this error message?

did you actually activate your virtualenv? run the commond like 'source yourenvdir/bin/activate'

if you have done this, maybe the package googleads is not suport your local python version

Hi Quitsmoking4ever,

many thanks for your help. I am quite a n00b so I will try to be specific. This is what I have just done:

  1. opened a bash console in /home/giancampo/.virtualenvs/AdWordsAPI/
  2. activated the "AdWordsAPI" virtualenv with the command you suggested ("source /home/giancampo/.virtualenvs/AdWordsAPI/bin/activate")
  3. left the bash console open in order to let the virtualenv active
  4. checked the Python version of the virtualenv with the command "which python"
  5. used the same reference in my script, where the shebang is "#!/usr/bin/python"
  6. got the same error as before :( but the googleads module seems to support any python version from 2.7 on (see link here)

Am I missing something? Any other idea of checks to do?

If the shebang is /usr/bin/python, then you're not running the Python in your virtualenv, you're running the system Python.

See http://help.pythonanywhere.com/pages/VirtualEnvInScheduledTasks/ about shebangs for virtualenvs.

Also, activating a virtualenv in one console does nothing for anything else. It is only active in the console where you activated it.

Hi Glenn, thanks for your kind reply. The only thing I haven't caught in your reply is this:

Also, activating a virtualenv in one console does nothing for anything else. It is only active in the console where you activated it.

So you mean my virtualenv could still be inactive? What should I do for it to be active?

P.S. Let me just explain, the only thing I want to do is running a Python script inside a virtualenv because I need the googleads module (unfortunately it is not one of your included batteries).

There are two ways to install modules which you can see here.

What you are doing when you create a virtualenv is that you create a copy of the system python, and when you pip install inside of the virtualenv you add new libraries to that copy of the system python.

You could also use the pip install --user flag to directly affect the libraries available to the system python (ie. /usr/bin/python).

You could also create multiple virtualenvs that go with different projects (eg: if this project needs django v11, the other one needs django v9, then multiple virtualenvs help manage this).

So by default if you just run a python program, it runs it using the system python. So how do you tell bash/your webapp/your scheduled task etc that you actually want the particular virtualenv python instead of the system python?

There's a couple ways to do this. eg: running source /home/giancampo/.virtualenvs/AdWordsAPI/bin/activate" as you mentioned will "activate the virtualenv" by tell the particular console that you just ran the activate script it to do this substitution and from now on, when it sees python, it will run the virtualenv python located at /home/giancampo/.virtualenvs/AdWordsAPI/bin/python instead of the system python.

But note that this only substitutes the python within that particular console. So for example, if you have also created a webapp, you can set your virtualenv settings in one of the webapp config tabs (scroll down until you find it) so we know which python you want to use, so we willl run your webapp with that python. If you start a new console, you will need to activate the virtualenv in the new console again.

Or if you are using scheduled tasks, then you want to make sure you have specified the correct python as well. There are a couple ways of specifying that. For example, you could make the scheduled task command be

/home/giancampo/.virtualenvs/AdWordsAPI/bin/python /path/to/my/script.py

Or you could just have

/path/to/my/script.py

but then within the file, use the #! technique to specify which python version you want. So your case it might be #!/home/giancampo/.virtualenvs/AdWordsAPI/bin/python. Glenn was commenting that #!/usr/bin/python would be the system python you are telling it to run.

Notably, if you do a which python after you have the virtualenv activated, you should see your python being #!/home/giancampo/.virtualenvs/AdWordsAPI/bin/python, and not #!/usr/bin/python.

Hello Conrad.

Many thanks for your support. I believe I did got it. Now I have other issues but they depend on the googleads module itself :)

However everything is clear to me now. You guys really rock. I am very happy to use PythonAnywhere!