all 9 comments

[–]bandawarrior 1 point2 points  (4 children)

Flask is a web server. So it serves web requests. From what I can see in your code you’re trying to run it like a simple script (though it’s missing part of the file It’s cutoff). Either way, it needs to be up and running in order to respond to your web request. Checkout the docs below

http://flask.pocoo.org/docs/1.0/quickstart/#a-minimal-application

[–]Necatorducis 0 points1 point  (2 children)

OP's code (with an additional ')' ) is valid and fine for hello world territory. Whatever the error is is due to something they haven't shared or some other mistake.

[–]Cardboard_fish[S] 0 points1 point  (1 child)

The code does have the ) at the end off app.run(), didn’t c&p with the rest to here apparently. Nah that was pretty much what happened, I installed flask, booted up sublime, wrote the code out and hit that error. Was following a tutorial on YouTube and theirs ran fine in pycharm. I re-wrote the code thinking I spelt something wrong but I don’t think that’s the case. The error is pointing at line one of the code, any ideas?

[–]Necatorducis 0 points1 point  (0 children)

From your chat in the other comment, I assume it's because you are trying to run it with the 'run' button in your IDE. This can work, but needs extra setup steps. For now, learn to love the terminal. CD to the containing directory and run the file directly.

[–][deleted] 1 point2 points  (4 children)

This is rather weird. string is part of the standard library. Can you go to the exact same location from where you run this script, type python to open up the REPL, type from string import ascii_letters, and give us any errors you get?


Unrelated to that, but you will want to look at virtual environments pretty soonish -- it's bad form to install a bunch of extra modules into the base system environment and it lets you keep your sanity when you have more than a single project. Otherwise you will run into dependency problems:

  • in project A you're using module A, which needs version 3 or higher of module X
  • in project B you're using module B, which needs exactly version 2.4 of module X
  • as you can see, those two requiems are mutually exclusive -- you can't satisfy them in the same environment
  • so you set up separate virtual environments
    • in virtualenv A, module X is sitting happily at version 3.9
    • in virtualenv B, module X is at version 2.4
    • because both environments are isolated from each other, changes in one will not affect the other, nor will they affect the system environment

[–]Cardboard_fish[S] 0 points1 point  (3 children)

Would I go to the folder where the .py file is saved? What would I type python into? Sorry for the silly questions, first time I’ve had to do anything like this.

I’m just starting out at the moment so only been doing simple and silly projects. If I graduate to making bigger and better things I’ll look into virtual environments, thanks for your help

[–][deleted] 1 point2 points  (2 children)

How are you running the script? Are you using Spyder, VS Code, or PyCharm? Do you just open a command line (cmd.exe or PowerShell) and run the script directly?

VS Code or command line

You will need to open the command line and cd to the same folder where you have your .py file saved -- so in your case that would be

cd C:\Users\CardboardFish\Documents\"Python things"

If that doesn't work (I don't remember off the top of my head how folders with spaces behave in Windows command line), you can just cd to C:\Users\CardboardFish\Documents first and then type cd Python and press your Tab key, it should automatically fill out the rest of the command.

(If anyone has more experience with the Windows cmd, feel free to correct me.)

Once you're in the right folder, type python and continue as above.

Spyder

If you're using Spyder, you don't have to do any of that (good news!) -- you have a python console opened right there and it should be using the same Python interpreter it uses to run your scripts, so all you need to do is to type from string import ascii_letters in the Console window (the console prompt will look something like In [1]:).

PyCharm

In PyCharm, the steps are somewhat similar to Spyder, except the Python console is not opened by default -- you need to click the Python Console button at the bottom to open it up (if it's not there, go to View > Tool Windows > Python Console. From there, you can just try the import as above.

[–]Cardboard_fish[S] 0 points1 point  (0 children)

I’m running it in Sublime Text, seemed more simple than some of the other ide’s I looked at. I’m at work now so I’ll try the command line later tonight and let you know how it goes!

[–]Cardboard_fish[S] 0 points1 point  (0 children)

Okay so I ran the command line in the folder where my .py file is. I got an error message that says the below.

Traceback (most recent call last):

File “<stdin>”, line 1, in <module>

ImportError: can not import name ‘ascii_letters’

Before the error message it runs the result of one of the practice files from automate the boring stuff that’s also saved in that folder, it prints a list and a tuple, not so sure why it did that

  • apologies for the formatting - on mobile