2

I've tried to find answers like the ones here, but they don't work for me.

I have a python3.6 environment called py36. If I conda activate py36 and then run flask with

export FLASK_APP=main.py
flask run --port=5050

I get the following error:

Traceback (most recent call last):
  File "/Users/USERNAME/anaconda/bin/flask", line 6, in <module>
    sys.exit(flask.cli.main())
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/flask/cli.py", line 513, in main
    cli.main(args=args, prog_name=name)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/flask/cli.py", line 380, in main
    return AppGroup.main(self, *args, **kwargs)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/flask/cli.py", line 423, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/flask/cli.py", line 152, in __init__
    self._load_unlocked()
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/flask/cli.py", line 176, in _load_unlocked
    self._app = rv = self.loader()
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/flask/cli.py", line 237, in load_app
    rv = locate_app(self.app_import_path)
  File "/Users/USERNAME/anaconda/lib/python3.5/site-packages/flask/cli.py", line 90, in locate_app
    __import__(module)
  File "/Users/USERNAME/Documents/Google_Drive/Projects/project/main.py", line 3, in <module>
    import MySQLdb
ImportError: No module named 'MySQLdb'

The issue isn't that there is no 'MySQLdb' installed, because it is installed under that environment.

if I do conda list I get

mysql-connector-c         6.1.11               h42a37c1_0  
mysqlclient               1.4.6            py36h0a44026_0  

which confirms that mysql is installed on this environment.

and if I run python I get

$ python
Python 3.6.1 |Anaconda custom (64-bit)| (default, May 11 2017, 13:04:09) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

which confirms that default python is 3.6.

As you can see everything is supposed to be python3.6, but the error comes from my python code for some reason launching python3.5 instead of python3.6. I don't have any prefixes or any headers telling which python it is supposed to run.

In fact this is the top of my main.py

from flask import Flask, request, render_template, jsonify
from flask_cors import CORS
from flask_pymongo import PyMongo
import MySQLdb
import json
import math
import pandas as pd
import numpy as np
from collections import OrderedDict
import pprint
import sys

app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})

In other words, my conda environment, which is supposed to safely contain a single python version (python 3.6), is failing to activate the correct python version inconsistently.

I'm scared to modify the paths but this is what I get if I echo $PATH

>>>echo $PATH
/Users/USERNAME/anaconda/envs/py36/bin:/usr/local/opt/openssl/bin:/usr/local/opt/openssl/bin:/usr/local/opt/openssl/bin:/usr/local/opt/openssl/bin:/usr/local/fsl/bin:/Users/USERNAME/anaconda/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands

The only way I fixed it is if I did the following:

conda activate /Users/USERNAME/anaconda
pip install pymysql

While this fixes my problem, it's a bandaid and it is not solving the issue. Because the base environment is not the one I want to be using when I conda activate py36.

Any help would be deeply appreciated!

Thank you!

davidism
  • 121,510
  • 29
  • 395
  • 339
appleslice
  • 31
  • 3
  • I haven't worked much with `conda`, more of a `pip` user, but have you tried importing that library from inside your Python session? What does it say? – Cerberton Jul 11 '21 at 10:30
  • What is the output of `which flask`? – Matt Thompson Jul 11 '21 at 16:41
  • @Cerberton What do you mean Python session, like within a notebook? I do in fact try to use pip (or conda) when i'm inside the conda environment (which is what I've been doing) @Matt this is what I get with `which flask`, `/Users/USERNAME/anaconda/bin/flask` – appleslice Jul 12 '21 at 16:21
  • @appleslice - I'd start with your virtual environment, that seems to be the issue here. I'd suggest creating a completely fresh virtual environment (let's call it "appleslice" for fun) `conda create --name appleslice python=3.6`, then do `activate appleslice` and proceed to install all your required packages. I think that's the best way to go about solving this. – Cerberton Jul 12 '21 at 17:35
  • The output of `which flask` is key to debug here, it is very likely flask is being picked up in a different conda environment. Installing flask in this conda environment would probably solve the problem. – William D. Irons Jul 12 '21 at 17:59

1 Answers1

0

My suggestion would be for you to re-create your virtual environment.

Let's create a completely fresh virtual environment (let's call it appleslice for fun):

First, make sure you are not currently running in any virtual environment by executing deactivate. Then run this:

conda create --name appleslice python=3.6

Then execute: activate appleslice and proceed to install all your required packages.

If still stuck, have a look at this article.

Cerberton
  • 376
  • 2
  • 7
  • 16