1

I just download a web application which written using Python. Im completely new to Python. After i do some research, below is what i have done

1) Install Python 2.7

2) Install pip (How do I install pip on Windows?)

When i try to run the python file by using this command

Python PATH/test.py

It show

Traceback (most recent call last):
File "PATH\test.py", line 1, in <module>
from flask import Blueprint, flash, request, render_template

Python Code:

from flask import Blueprint, flash, request, render_template
from steam import vdf
import json

vdfjson = Blueprint("vdfjson", __name__, template_folder="templates")


@vdfjson.route('/', methods=["GET", "POST"])
def index():
response = None
format = "json"
if request.method == "POST":
    format = request.form["format"]
    data = request.form["data"]

    try:
        if format == "vdf":
            response = json.dumps(
                vdf.loads(data),
                indent=4
            )

        elif format == "json":
            _response = json.loads(data)
            response = vdf.dumps(_response).decode("utf-16")

    except ValueError:
        flash("ValueError:  Your {} may not be valid.".format(format), "danger")
        response = "{}" if format == "json" else ""

return render_template("vdfjson.html", response=response, format=format, title="vdfjson")

*It is a web application, so im not sure whether i follow the right instruction or not.

I try to install flask

pip install flask

and i get below error

C:\Python27\Scripts>pip install Flask
Downloading/unpacking Flask
Downloading Flask-0.10.1.tar.gz (544kB): 544kB downloaded
Running setup.py egg_info for package Flask
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'include_package_data'
  warnings.warn(msg)
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'zip_safe'
  warnings.warn(msg)
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'install_requires'
  warnings.warn(msg)
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'test_suite'
  warnings.warn(msg)
usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: -c --help [cmd1 cmd2 ...]
   or: -c --help-commands
   or: -c cmd --help

error: invalid command 'egg_info'
Complete output from command python setup.py egg_info:
C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution opt
ion: 'include_package_data'

warnings.warn(msg)

C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option:
'zip_safe'

warnings.warn(msg)

C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option:
'install_requires'

warnings.warn(msg)

C:\Python27\lib\distutils\dist.py:267: UserWarning: Unknown distribution option:
'test_suite'

warnings.warn(msg)

usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]

or: -c --help [cmd1 cmd2 ...]

or: -c --help-commands

or: -c cmd --help



error: invalid command 'egg_info'

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in c:\users\sam\appdat
a\local\temp\pip_build_SAM\Flask
Storing complete log in C:\Users\SAM\pip\pip.log

C:\Python27\Scripts>

After that i execute again

Python PATH/test.py

it did not show any error but do nothing.

Community
  • 1
  • 1
My2ndLovE
  • 287
  • 4
  • 18

5 Answers5

1

It is not a complete flask application. You need to make Flask instance and then register your blueprint in it. Try to run the code below:

from flask import Blueprint, Flask, flash, request, render_template
from steam import vdf
import json
app = Flask(__name__)


vdfjson = Blueprint("vdfjson", __name__, template_folder="templates")
app.register_blueprint(vdfjson)


@vdfjson.route('/', methods=["GET", "POST"])
def index():
    response = None
    format = "json"
    if request.method == "POST":
        format = request.form["format"]
        data = request.form["data"]

        try:
            if format == "vdf":
                response = json.dumps(
                    vdf.loads(data),
                    indent=4
                )

            elif format == "json":
                _response = json.loads(data)
                response = vdf.dumps(_response).decode("utf-16")

        except ValueError:
            flash("ValueError:  Your {} may not be valid.".format(format), "danger")
            response = "{}" if format == "json" else ""

        return render_template("vdfjson.html", response=response, format=format, title="vdfjson")

if __name__ == '__main__':
    app.run()
Alexander Zhukov
  • 3,859
  • 1
  • 17
  • 30
1

You'll need to install flask (and possibly some other libraries). Start with flask and see what errors you get after.

pip install flask

Often there's a requirements.txt file with the project that has the list of dependencies. You can then just run:

pip install -r ./path_to/requirements.txt

Which will install them all for you. Once you're more comfortable look into virtualenv which will allow you to create isolated environments for installing your libraries on a per project basis.

Aidan Kane
  • 3,487
  • 1
  • 22
  • 28
  • I get some error during installation of flask. Please refer to the updated post. – My2ndLovE Oct 11 '13 at 13:23
  • 1
    Looks like you have some issue with the setup tools. Apparently you need to run 'pip install --upgrade setuptools' (then try again). http://stackoverflow.com/questions/11425106/python-pip-install-fails-invalid-command-egg-info – Aidan Kane Oct 11 '13 at 13:31
  • Thanks. I re run using "Python PATH/test.py" this time no more error but nothing show. – My2ndLovE Oct 11 '13 at 13:33
  • Sounds like now you want to try @Alexander Zhukov's solution above. The test.py that you showed us is for a Blueprint - it's like a submodule of an application that expects to be included in something bigger. You really need the application itself. I have no idea what the project is but I would have thought there would be a main application file with it somewhere. – Aidan Kane Oct 11 '13 at 13:42
  • Just to add - once it's running it should look the terminal has frozen and you'll be able to access it in your web browser by going to localhost:5000 – Aidan Kane Oct 11 '13 at 13:43
1

The author has update the latest version of the original source source. So it work right now. Case closed. Thanks.

My2ndLovE
  • 287
  • 4
  • 18
0

You successfully run your test.py,
however libraries

from flask import Blueprint, flash, request, render_template

you try to import is not in PYTHONPATH

asdf_enel_hak
  • 7,114
  • 3
  • 36
  • 77
0

This worked for me:

pip install -u setuptools

pip install flask

nu everest
  • 7,371
  • 9
  • 62
  • 87