How to make flask run the launch of Python 3 instead of Python2.7?

0

I tried to launch a Flask app, which you can find on GitHub . Surprised it starts with Python2.7 now, while it launched with Python3 before. It did not work very badly. I do not know what the reason is.

(MyFlaskAppEnv) mike@mike-thinks:~/Programming/Rasa/myflaskapp$ flask run
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "/home/mike/.local/bin/flask", line 11, in <module>
    sys.exit(main())
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/mike/.local/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 767, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 293, in __init__
    self._load_unlocked()
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 317, in _load_unlocked
    self._app = rv = self.loader()
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 377, in load_app
    raise_if_not_found=False)
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/home/mike/Programming/Rasa/myflaskapp/app/__init__.py", line 42
SyntaxError: Non-ASCII character '\xc2' in file /home/mike/Programming/Rasa/myflaskapp/app/__init__.py on line 42, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

In fact:

(MyFlaskAppEnv) mike@mike-thinks:~/Programming/Rasa/myflaskapp$ python -V
Python 2.7.12
mike@mike-thinks:~/Programming/Rasa/myflaskapp$ python3 -V
Python 3.5.2

Update

I tried to specify the version of Python I wanted in the environment, but it looks like it was already Python3.

mike@mike-thinks:~/Programming/Rasa/myflaskapp$ virtualenv MyFlaskAppEnv/ --python="/usr/bin/python3"
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/mike/Programming/Rasa/myflaskapp/MyFlaskAppEnv/bin/python3
Traceback (most recent call last):
  File "/home/mike/.local/bin/virtualenv", line 11, in <module>
    sys.exit(main())
  File "/home/mike/.local/lib/python3.5/site-packages/virtualenv.py", line 712, in main
    symlink=options.symlink)
  File "/home/mike/.local/lib/python3.5/site-packages/virtualenv.py", line 927, in create_environment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "/home/mike/.local/lib/python3.5/site-packages/virtualenv.py", line 1233, in install_python
    shutil.copyfile(executable, py_executable)
  File "/usr/lib/python3.5/shutil.py", line 98, in copyfile
    raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
shutil.SameFileError: '/usr/bin/python3' and '/home/mike/Programming/Rasa/myflaskapp/MyFlaskAppEnv/bin/python3' are the same file
mike@mike-thinks:~/Programming/Rasa/myflaskapp$ source MyFlaskAppEnv/bin/activate

So when I run, it gives me the same error.

(MyFlaskAppEnv) mike@mike-thinks:~/Programming/Rasa/myflaskapp$ flask run
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
Traceback (most recent call last):
  File "/home/mike/.local/bin/flask", line 11, in <module>
    sys.exit(main())
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/mike/.local/lib/python2.7/site-packages/click/decorators.py", line 64, in new_func
    return ctx.invoke(f, obj, *args[1:], **kwargs)
  File "/home/mike/.local/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 767, in run_command
    app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 293, in __init__
    self._load_unlocked()
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 317, in _load_unlocked
    self._app = rv = self.loader()
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 377, in load_app
    raise_if_not_found=False)
  File "/home/mike/.local/lib/python2.7/site-packages/flask/cli.py", line 235, in locate_app
    __import__(module_name)
  File "/home/mike/Programming/Rasa/myflaskapp/app/__init__.py", line 42
SyntaxError: Non-ASCII character '\xc2' in file /home/mike/Programming/Rasa/myflaskapp/app/__init__.py on line 42, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
    
asked by anonymous 01.08.2018 / 18:18

3 answers

0

So I see you know how to create a virtual environment just not specifying the Python you want to use in your environment do the following in ubuntu:

which python3

It will return something like:

/usr/bin/python3

Then when creating the virtual environment specify the Python path you want to use (in the case of Python 3) as follows:

virtualenv <nome_do_ambiente_virtual> --python="/usr/bin/python3"

Now you can start the virtual environment and it will use Python 3.

    
01.08.2018 / 18:47
0

I tried to create a new virtual environment for Python 3:

$ virtualenv --python='which python3' flaskenv
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/cesar/flaskenv/bin/python3
Also creating executable in /home/cesar/flaskenv/bin/python
Installing setuptools, pip, wheel...done.

I should have version 3:

$ source flaskenv/bin/activate
(flaskenv) $ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

So I installed the dependencies normally and tried again by lifting the application.

$ pip install -r requirements.txt
...

And that's it!

    
01.08.2018 / 21:49
0

Open your terminal and run the following command:

First check if you have python 3 installed: python3 -V

sudo pip3 install pipenv

Now create a folder of your choice and within it execute the command:

pipenv --three

Now you can install flask normally

pipenv install flask

Pipenv is the new package manager for python, it would be nice to have a look at the documentation.

    
09.09.2018 / 15:51