Forums

virtualenv and git

I'm encountering a problem with my Flask-based webapp. When I first joined PythonAnywhere, I went through the first walkthrough to create the database-powered comments webpage with source control via git. Felt good.

I then branched out to a personal project, but was lazy and didn't use git regularly. I created a virtual environment to get a newer version of a package and that all went well. The webapp worked as expected.

Yesterday, I went into that first walkthrough again to refresh my memory about how to use databases in python because I'd love to make use of them in my personal project. As I went through the instructions, I was ashamed as I remembered I had been neglecting source control, so I went ahead and did git commit to get myself caught up.

Then the webapp stopped working. I now get an error:

2017-12-21 21:50:16,322: Error running WSGI application
2017-12-21 21:50:16,344: ModuleNotFoundError: No module named 'twilio.twiml.messaging_response'; 'twilio.twiml' is not a package
2017-12-21 21:50:16,344:   File "/var/www/rclark010_pythonanywhere_com_wsgi.py", line 16, in <module>
2017-12-21 21:50:16,345:     from flask_app import app as application
2017-12-21 21:50:16,345:   File "/home/rclark010/quiksite/flask_app.py", line 98, in <module>
2017-12-21 21:50:16,345:     from twilio.twiml.messaging_response import MessagingResponse

Again, the code worked before I committed. The virtual environment contains a newer version of Twilio (6.10.0) than the default PythonAnywhere Twilio package, which does not have the twilio.twiml package. That makes me think that my git commit screwed up the virtual environment somehow.

After some research online, I've found that when setting up a virtual environment, there are some steps one should take to make git and the virtual environment play nice. Something about pip freeze and pip freeze > requirements.txt and pip install -r requirements.txt. I've seen different commands in different places, all without much explanation.

I neglected to do any of those steps before committing my code to git yesterday. (Ah, to be so young and naive.) My questions are:

  1. Is that likely the source of my error?
  2. If so, what's the best way to fix the situation? I think I can just delete the virtual environment and start fresh, but not sure if there is a more elegant solution
  3. Can you point me in the direction of a good guide that explains how to properly set up a virtual environment with source control for future reference? My searching thus far hasn't borne much fruit.

Thank you very much in advance for any help you might be able to provide! Cheers, Clark.

  1. No, probably not. A git commit does not change files
  2. Make sure that you have the correct version of twilio installed in the environment (python version, virtualenv) that you're using for your web app.
  3. virtualenvs should not be in source control. pip freeze generates a list of the packages installed in the virtualenv. If you put that in a file with pip freeze > requirements.txt, then you have a file that you can commit so you can reinstall the required packages into a virtualenv when you need to with pip install -r requirements.txt

Alright. So, first, your third point was quite helpful. I just needed those commands explained in relatively simple terms and now I understand them. The commands don't actually quarantine the virtual environment, they just make it easy to rebuild an environment elsewhere if needed in the future. Great. Thank you!

Your first point was helpful, too, by informing me that my diagnosis was incorrect. I had already checked the version of python and twilio in my virtual environment as a troubleshooting step (as described in your second point), and they seemed fine.

So I dug deeper into the error logs. Turns out I was reading them incorrectly. A newbie mistake. I didn't realize that newer errors were added to the bottom of the page; I was reading as if they were added to the top. So I thought I was getting the same error over and over, when, in reality, I was just reading the same thing over and over.

Turns out my actual error was ModuleNotFoundError: No module named 'mysql'. That's not a module listed on the Batteries Included page, so I searched the fora and found a helpful posting that said to install mysql-connector-python. Did that, and everything worked. The module mysql must be in that package. Beautiful.

Now I have a working web page, know how to read an error log (duh), and know how to add requirements to my git. Thank you very much, glenn.

Excellent, glad we could help! The confusion about virtualenvs is quite common; basically, although they isolate your environment from the rest of the computer you're running on (so you can different versions of packages, etc.), they're not portable from computer to computer.