Forums

ModuleNotFoundError - I have installed the module in my virtual environment, but can't import it

I am running this file: /home/Programmer1/.virtualenvs/myvirtualenv/scratch.py

This is the code:

import gspread
print('hi')

This is the error: Traceback (most recent call last): File "/home/Programmer1/.virtualenvs/myvirtualenv/scratch.py", line 3, in <module> import gspread ModuleNotFoundError: No module named 'gspread'

I have written the directory on the Web tab: /home/Programmer1/.virtualenvs/myvirtualenv

I installed the module in Python3.8 and I have the code set to Python version 3.8 in the Web tab.

Am I missing a step to be able to import the module to my programme?

I think you might be misunderstanding how virtualenvs work -- the actual location where you store your script isn't related to which virtualenv uses. You could have a virtualenv in /home/Programmer1/.virtualenvs/myvirtualenv, and a file in /home/Programmer1/something/scratch.py, and still run the file using the virtualenv -- but you would need to activate the virtualenv first in order to run it.

  • To activate a virtualenv for your website, you just specify it on the "Web" page, like you've already done.
  • If you're starting a Bash console, and then using something like python /home/Programmer1/.virtualenvs/myvirtualenv/scratch.py to run the script, then you would need to run the command workon myvirtualenv first, and only then use python to run the script.
  • If you're running the file from the editor that you can load up from the "Files" page, then you can specify the virtualenv using a hashbang.

I probably am, I am beginner. Thank you, I will try this now.

OK -- let us know how you get on!

Thank you so much, Giles. This stuff is hard if you haven't learnt it already. I started learning Python a few weeks ago and now am getting close to running a programme on an online server. Your website is great.

I've got the hashbang in my file and now there are no import errors! Now I will just upload my programme and try and get it running.

Edit: I've got a very basic programme working. I've managed to edit a google sheet using the programme running on here. I could cry with joy, haha.

yayy!