Flask mega tutorial part 5 error - python

Im going through the Microblog tutorial for Flask designed by Miguel.
I made it to part 4 without any issues. Everything ran in the end of part 4.
I started having issues in part 5. After a couple of tries and trying to troubleshoot with the comments in the page, I was not able to finish the exercise.
Thinking it was me, I deleted all of my project and replaced them with the zip downloadable in the page for the exercise. I still get the same error, so the problem is not the code itself.
I get the following error when running views.py:
Link to image on imgur (couldn't post it here due to not having enough reputation)
Here are the libraries I have installed:
Link to image on imgur (couldn't post it here due to not having enough reputation)
My setup:
I am using Pycharm to run the files. This machine runs Windows and I found that Pycharm is the most efficient way to run things without dealing with issues with the cmd line.
Python 3.4
virtualenv created by Pycharm
the code can be found on github: github.com/pdgonzalez872/microblog
Potential cause of the problem:
The problem may be with the path that I have set up (but why did it work until part 4?)
The code itself (maybe something was updated in flask/other libraries since the post?)

views.py isn't meant to be launched separately, there is run.py file to start the project.
Also, it uses relative imports (those with dots before module names), which don't work when you use them in a script passed to the interpreter and used as main. Sorry, I can't explain it well, but maybe you will be interested in those links:
https://docs.python.org/2/tutorial/modules.html#intra-package-references
How to do relative imports in Python?

Related

How to redirect a python.exe within a pre-existing venv's .env file

Recently I uninstalled python and then reinstalled it to see what affects it would have on my code and ensure that I knew how to fix any path or directory issues. I mostly succeeded but am having trouble within my venvs. Testing out a basic django app I made any of my python manage.py ... code returns the error No Python at '"C:\Python311\python.exe'.
After doing some research it appears that the issue is that within .env\scripts there are two files called "python.exe" and "pythonw.exe" are causing the problem because they are trying to access a python.exe that no longer exists. (Note this is my understanding based off of pythons venv document and I could be incorrect)
My question is what is the best way to deal with this issue. I see two options but am not sure how to accomplish either.
Fully reset the .env folder so it points to the correct python.
Just change those two files so they point to the correct instance of python.
So far the only things I have tested are running code outside of venv's entirely and creating new ones to run the code from. Both work totally fine so it is definitely something from the historic venvs.
I have also looked at this question and I believe the two to be related but could be mistaken. Unfortunately it does not seem an answer was given there.

How to solve ModuleNotFoundError: No module named 'string' error in Python 3?

I was trying to add the Strings library into the Robot Framework Interpreter folder in PyCharm, where first it showed me error
Command errored out with exit status: 1
So, I googled out this issue first, and this link suggested me to delete 'strings.py' file from the libraries. I did so and now nothing is working.
Anything I do now, it shows error of "ModuleNotFoundError: No module named 'string'".
I could not even install string using
pip install strings
command.
Whatever I try to do with pip, now shows this error.
Can anyone please suggest me the solution for this?
I am using Python version 3.10.
Several notes:
The library in question is 9 years old, is not maintained, is known by the author to be buggy, was written as a joke, and does not contain anything useful. There is no good reason why you should be trying to install it for your project. If you think you need it for something, then you have some other misconception that needs to be cleared up.
The link you found did not tell you to delete strings.py from "the libraries". It said something about deleting string.py - notice, no s at the end - from a local project folder. The reason for this is because that name conflicts with the library name. The page author's own source file, named string.py, sought to import the standard library file string.py (as it clearly says import string in the screen shot), but it cannot - because it finds itself first. This is a common problem for new Python users.
DO NOT EVER MANUALLY EDIT THE CONTENTS OF YOUR INSTALLATION DIRECTORY ON THE ADVICE OF SOME RANDOM WEB PAGE.
Ideally, don't ever do it at all. That content is not intended to be touched. Installers exist for a reason. If for some incredibly specific reason you feel the need to do this, make sure you have backups of everything and that you are 100% sure you can restore everything to its initial state if anything goes wrong.
The person writing that web page was incorrect. The installation error had nothing to do with the string.py file.
The actual cause of the problem is that the package is broken and cannot be installed properly on anyone else's machine. Again, this is no big loss as there is no use for the package anyway.
The reason it is broken is that the setup script for the package tries to import the code that's being installed, in order to get version and author information. This seems to work locally, but fails for everyone else.
To reiterate: the person writing that article wrote nonsense. (I'm not surprised; the page formatting is awful and the grammar isn't particularly great either.) Looking further, it appears that the entire website is authored by the one person, who is clearly just trying to self-promote (with a Youtube channel as well) while lacking the necessary expertise. Browsing around the rest of the site a bit, I see articles that are pedantic and not very insightful, and occasionally inaccurate - but all very SEO optimized.
I recommend ignoring that website entirely.
To reiterate: the string module comes with your Python. You cannot reinstall it with pip - not with the strings package you found, nor any other package. Your options are:
Find the correct string.py contents (possibly from a backup, assuming you thought to make a backup before deleting something from an installation directory) and restore them. This is the official repository for the reference implementation of Python. You might be able to find it in there somewhere. I don't recommend trying. There is a lot to go through and it is possible to damage things further.
Reinstall Python completely.
The error that you are getting is because you deleted the string.py file. There is no string module to be imported from PyPi. This is why pip install string doesn't work. Restore the deleted file as it is not the cause of your problem.
If you try to install Strings library it will fail because you are using Python 3.10 and the Strings library that you want to import and install is quite old and not supported for this Python version. Therefore you get the ModuleNotFoundError: No module named 'strings' that you see in the link that you attached.
For the setup.py file from the library I see that it recommends using Python 3.3, so I will recommend you to use that version of Python if you want to use this specific library.
Please note that the link that you provided is probably in case that you have created your own string.py file.
I solved this question by deleting any __pycache__ folder in the project directory.

Getting "Premature end of script headers" on Dreamhost Python Webpage

I have a webpage made in Python Flask with Python 2.7 (I know), hosted on Dreamhost. Up until a few days ago I had absolutely no problems. Then, I started getting a permission error on my page. I don't know what happened, but I got with Dreamhost live chat and they solved it by doing a chmod 755 to my files. Everything was working again, but on further inspection, all the parts that generate a PDF document in my webpage have stopped working. The error that I am getting in the error logs is "Premature end of script headers".
This error only appears whenever I try to generate a PDF. The code works perfectly on my local environment, but it does not work on the hosting. I got with the people of Dreamhost again and they said that it was nothing on their side. Which is weird considering that the code that was ALREADY WORKING was not changed at all. I tried debugging on my part but I am not getting any errors.
I have tried pretty much everything I can think of to solve this situation but I have been unsuccessful. The course of action is to change this hosting (it was a mistake on my part to use Dreamhost in the first place, I know. But when I made this I was just starting and I had very little knowledge). But I need a quicker solution for the moment, and I was wondering if anyone has had this problem and could maybe help me out with this.
To elaborate, I am using Python 2.7, the library that is used for the PDF creation is Flask-Weasyprint (0.5). I tried running the wsgi file myself, and I am getting the following message:
/path/to/local/lib/python2.7/site-packages/WeasyPrint-0.39-py2.7.egg/weasyprint/text.py:29: UserWarning: There are known rendering problems with Cairo <= 1.14.0
warnings.warn('There are known rendering problems with Cairo <= 1.14.0')
/path/to/local/lib/python2.7/site-packages/WeasyPrint-0.39-py2.7.egg/weasyprint/fonts.py:46: UserWarning: #font-face support needs Pango >= 1.38
warnings.warn('#font-face support needs Pango >= 1.38')
I know that I should change to Python 3. When I made this web app, Dreamhost only accepted Python 2 and I barely had any knowledge of creating VPS or something like that. It is my plan to change hosting soon to a virtual machine and to run the project in Python 3. But I need a solution for this now before I go on.
Any help will be appreciated. If you need more details please let me know.
This may be due to the Passenger file not including your project directory in its path. One solution is adding the following to your passenger_wsgi.py file:
sys.path.append(os.getcwd()+"/<project_name>")

Running Python Example Code

I apologise if this is too basic for StackOverflow, but I am currently running Anaconda v3.6.4 and I am trying to get the first example code from the ML: A Probabilistic Perspective GitHub to work but I have no idea how.
On the GitHub read-me, it says;
"You should run the code from the repo root ('pyprobml' directory) using module execution, e.g.
python -m examples.LMSdemo
The example I am trying to run is in examples/mnist1NNdemo.py in the GitHub directory
I have tried using the Anaconda Prompt, Spyder and even rewritten the code but I can't see to get it to run this line / code.
I wish I could provide an example but I am unsure how to even open the code correctly!
If you can share how to get the code to work using Anaconda, then I'd be so grateful - I'm at my wits' end!
https://github.com/probml/pyprobml
Ok, so in the future you would be advised to break your question down into smaller parts. The problem you require an answer to in your question is a different one then your current error (as you mentioned only in the comments to your question) is indicating.
The error you are getting (python is not recognised) already has an answer [here][1]. Which I will not repeat here since it does not relate to your question.
Once you've managed to get Python added to your PATH simply navigate to the folder C:\pyprobml-master (assuming this is indeed the checkout location of the repo on your system? - I am assuming based on the comments to the main question) and execute the demo via python -m examples.mnist1NNdemo.
As an alternative you could also navigate to C:\pyprobml-master\examples and run python mnist1NNdemo.py

Python3/MacOSX integration into pycharm

I have had trouble setting up the pycharm ide on my macosx10.7 with python3..
I have scoured every resource available and tried hundreds of approaches, at this point I must accept my incompetence and seek help via this channel.
In my research, I notice a lack of ground-up explanations on python integration into macosx and how to configure pycharm to import modules, run code within the editor, etc. If i ever solve this I will make a very detailed tutorial.
I have imported python3 successfully, it looks like it is linked appropriately from /sys/lib/frameworks to /usr/lib ...etc -- version control is working just fine.
I think my issue is either in setting environmental variables (tried the program to fix this and tried macports) and in the script needed to execute. it will catch errors throughout but final product does not run in python and returns printout of :
/Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2 /Users/anon/Desktop/pythonpractice/Py_Ex/classes.py
Process finished with exit code 0
i really need to get this configuration sound for my python programming class. please help (I've been through every line of pycharm website) .. preferably is there a way to map it via terminal? thanks for anyone who took the time to read this.
Summary of the discussion above:
Python 3.2.2 installation was broken on this Mac, installing ActiveState Python 3.2.2 from scratch and configuring it in PyCharm has fixed the problem.
Python path to be used in PyCharm settings: /Library/Frameworks/Python.framework/Versions/3.2/bin/python3
Incompatible third-party plug-ins may break PyCharm, uninstall/disable them in Preferences | Plugins.
Ensure the latest PyCharm version is installed.
User's code depends on the graphics.py module which was not in the project or in the PYTHONPATH. Putting it into the project has solved the problem.
Most likely the wrong Run/Debug configuration was used in PyCharm, the easiest way to run or debug such scripts is by using the editor context menu Run and Debug actions. PyCharm creates the configuration automatically and debugging works fine as shown of the screenshot:
If one wants to configure and debug it, he can use the code.zip file to get started.
Sorry for the comments mess above, but it was not possible to move it into chat as user had only 1 reputation point, hence not able to use the chat feature of StackOverflow.

Categories