简体   繁体   中英

How to prevent osx from using python flask 2.7

I have added the following alias to my ~/.bash_profile in order to run python3.8 instead of osx's default 2.7.: alias python=/usr/local/bin/python3 . So now, when I run python --version , I successfully see that I'm running python 3.8.

The problem is, when I run a flask application, it appears to still be referencing python2.7 in my system, as noted in the stack trace:

$ env FLASK_APP=server.py flask run
 * Serving Flask app "server.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "/usr/local/bin/flask", line 11, in <module>
    sys.exit(main())
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 966, in main
    cli.main(prog_name="python -m flask" if as_module else None)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/Library/Python/2.7/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Library/Python/2.7/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Library/Python/2.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 848, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 305, in __init__
    self._load_unlocked()
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/Library/Python/2.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/Users/vismarkjuarez/Documents/Github/Distributed-Systems/app/server.py", line 18
    return f'Welcome to Quiz API v1!'

I'm new to Python -- how do I get python 2.7 to stop being referenced?

You should try creating a virtual environment to check if this solves your problem

there's a varietey to achieve this, but most of them consist of creating a venv -> activating the venv and then starting the python program while the venv is active

Side-note: An IDE like Pycharm can be configured to activate the venv automatically to run your local server, using the "Configurations" dialog.

Not entirely sure how you have your profile configured or your specific environment variables but it is very common to use Virtual Environments. venv is built into python which will create a contained "environment" for your project.

You can create a virtual environment by calling

$python3 -m venv venv

This will create a folder in your current directory which you have to source.

$ source venv/bin/activate

This should change your terminal line to show you are in the environment. Then you can check to ensure you're using python three.

$ which python
>> /<path-to-current-directory>/venv/bin/python

You will notice you now need to reinstall all of your packages you want to use. That's because this is all self contained.

$ pip list

Package    Version
---------- -------
pip        19.0.3 
setuptools 40.8.0 

Once your pip install all of your packages again, and run your code while the virtual environment is activated, all python scripts will revert to this python version you created the environment with. It was creates specifically for times like this.

So if you want to follow along in your terminal, you can do the following. The dollar sign signifies a new command into the terminal and doesn't need to be entered.

$ python3 -m venv venv
$ source venv/bin/activate
$ pip install --upgrade pip
$ pip install flask==1.1.1

Then I created a small file called app.py with the following contents

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run() 

Then running

$ env FLASK_APP=app.py flask run 

starts the application in python3

Changing the alias in your ~/.bash_profile just changes the call when your personally type python into your terminal. It does not change where applications and programs will search for python. They use your environment variables and current the default python installation is python2. You can also change what interpreter your python script looks for by using a shebang line as the first line of your file

#!/usr/bin/python3

or

#!/usr/bin/env python

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM