Import for flask app not recognized by heroku - python

I am trying to use a .py file that I wrote called "sentence" as an import to index.py.
They are both in the app's main/home folder. To import, I am saying
import sentence
And the app works fine when I test in Komodo. But when I push to heroku, my application becomes an error page. The logging addon I have shows that the error is
ImportError: No module named sentence
I have tried numerous solutions suggested on many different places, and none have worked. I have tried adding __init__.py to the folder. I have tried reinstalling flask to my application and my computer. I have tried moving files to and from /static thinking maybe my files are static resources, but to no avail.
What else can I do to troubleshoot this problem?

I figured out the problem. At least for my situation. I forgot that linux and windows treat capitalization in filenames differently. Upon refactoring some elements of the app, I renamed 'Sentence' to 'sentence.' I discovered this by pulling a fresh repo on a different machine. To fix it, I moved the offending file out of the local repo folder, committed and pushed, then added it back in, committed and pushed. Went back to working as soon as I did that.
If you're having the same issue and this didn't solve it, I'm sorry! Get in touch with me and maybe by the time you're reading this I'll have more experience and can help you better than I helped myself.

Related

What Does PyCharm Do To Make It Easy To Import Other Modules?

I have a question regarding simple imports that I cannot get my head around.
Take a look at the attached screenshot to see my project layout.
The file somefile.py imports the class SayHello from a file called someclass.py and calls it. someotherfile.py does the exact same thing. They both use from someclass import SayHello.
In Pycharm both files run. However, From the command line or from VSCode somefile.py runs, but someotherfile.py errors out with the following error:
ModuleNotFound: No module named 'someclass'.
I believe it has something to do with PYTHONPATH/environment variables or something like that, but every explanation I have read has confused me thus far (Even this one which I thought was going to set me strait Relative imports for the billionth time).
Can someone explain in simple terms what is happening here? What is Pycharm doing by default that other editors are not such that my imported modules are found? How can I make someotherfile.py work in VSCode without modifying it?
Thanks in advance!
Pycharm adds your project directory into python paths by default. See configuration of "Pycharm run" that you execute and you shall see few checkboxes like
If those checked Pycharm creates PYTHONPATH environment variable for you that instructs Python where to look for someclass module.
You will have to configure VSCode to define PYTHONPATH environemnt variable for python command you run and include your root project directory path on it.
TLDR: Mess with the line starting with ./automated pointing it to various directories in your project until it works haha.
Long rambling answer: Alright now that I am not in a frenzy from trying to figure this out and it has been a day, I feel like I can make a conherint response (lets see if that is true).
So my original answer was an attempt to simplify my problem into what I thought was the issue due to a ModuleNotFound error I was getting. I had been trying to get unittests in Python inside of Visual Studio code to work (hint hint just use Pycharm it just works out of the box in there), but the integrated testing feature or whatever could not find my tests giving ModuleNotFound as the reason.
The solution to my problem actually just concerned the line ./automated-software-testsing-with-python/blog.
In the below screenshot the relevant part is ./automated-software-testing-with-python/blog.
This is it when it is correctly configured (My tests work Woo hoo!)
Now you can go ahead and read the official documentation for what this line is doing, but I am convinced that the documentation is wrong. Due to how my code is structured and how my files are named, what the documentation says its looking for definitely does not exist. But that is another can of worms...
I actually got it to work by accident. Originally when you go though the wizard to set up what's in that screenshot above, it defaulted to ./automated-software-testing-with-python which did not work. I then manually edited it to something else that was not the below screenshot hoping to get it to work.
It was only when I pointed it to that blog directory on accident (thinking I was in a different file due to trying to debug this for hours and hours in a blind rage) that it ended up working.
I did a bunch of PYTHONPATH manipulation and Environment Variable mumbo jumbo, and I originally thought that that had an effect, but when I cloned my repot to my other machine (which did not have any of that Environment Variable PYTHONPATH stuff going on) it worked too (again, provided that the line in question pointed to blog.
Ok hopefully that might help somone someday, so I'll end it there. Not to end on a bitter sounding zinger, but I will never cease be amazed by how doing such simple things such as configuring the most basic unit test can be so difficult in our profession haha. Well now I can start working. Thanks for the help to those who answered!

pdf2image not working with gunicorn/flask

For the past day I have been trying to incorporate pdf2image into my flask webservice.
The problem is, whatever I do, I still get the "Is poppler installed and in PATH?" error message.
I have written a little program that just prints the number of pages in a pdf file. This works completely fine, inside and outside the venv containing flask. If I try to do this from the flask file, I get the error. I even tried calling the very same function in the test code, but to no avail. I don't really know what to do anymore, so any help is greatly appreciated.
P.S.: I don't really know what source to include for this post, so just feel free to ask and I will provide it.
I had the symptoms which you describe. For me the problem was the following:
in the .service file used by systemd to start gunicorn, I had an incomplete path variable. The solution was to add the path to pdftoppm and pdftocairo, which in my case (on Debian 10) was /usr/bin
The solution is therefore that the .service file includes
[Service]
...
Environment="PATH=<your_app_path>/venv/bin:/usr/bin"
Note: This problem and its solution are thus broader than a pdf2image issue, but it seems to show up frequently with pdf2image... assuming this is true, I hope that your question and my response are both relevant.

My application won't run because I'm missing MSVCR90.dll, what can I do?

I used the instructions here to create my application. I get this message every time I double click my application after creating it with py2exe:
The program can't start because MSVCR90.dll is missing from your computer...
I don't know how I'm suppose to get this file: MSVCR90.dll.
I know this question has been asked before but I'll be honest the answers have left me more confused and also I'm seeing different answers everywhere. Using the py2exe tutorial it says I need version 9.0.21022.8 of the MSVCR90.dll so I use the link they give me. I download and install it but now I don't know what to do next as it doesn't seem to do anything aside from making a mess of my c drive. I tried creating the application again but the problem doesn't go away. I looked up in my computer to see if I have MSVCR90.dll and it takes me to a weirdly named folder which contains three .dlls: msvcm90.dll, msvcp90.dll, msvcr90.dll.
I copied this msvcr90.dll (version: 9.0.30729.1) to the dist folder and ran the application but it still doesn't work as it says: Runtime error ... R6034...
I even changed setup.py to add this line:
options = {"py2exe": { "dll_excludes": ["msvcr90.dll"]}} but that didn't work either.
Ok it worked. I had to include the images from the toolbar of my program within the dist folder. Aside from that you also need to exclude msvcp90.dll in setup.py.

Trac Plugin Not working

At work we are using Trac on several internal wiki's and an external wiki. REcently we found the need for a new plugin. After we going through a few tutorials we went to install a plugin to make sure it would work. It didn't. We've been going through trying to figure out. Below I will list the steps and various things I did while trying to get it to work.
1) I went to trac-hacks website and downloaded their hellow world plugin, figured I couldn't make a mistake using their code.
2) I compiled and made an egg using python setup.py bdist_egg on the machine where trac is installed, to make sure it's the same Python version being used.
3) I then copied it over to /directory/where/trac/is/plugins/ folder and chmod 755 the file egg file.
4) I then restarted http, unable to find a better way of restaring trac so this may be where my problem is. It didn't work. So I deleted the egg folder in plugins
5) Uploaded it via trac->administration->plugins and restarted httpd again. Nothing.
6) I realized I had to edit the trac.ini file and added helloworld.* = enabled under component and restarted the web server.
It's quite possible it's me but any help would be greatly appreciated!
Its the helloworld plugin from trachack, essentially displays hello world and theres a button. There are no error messages provided, hence why googling was hard.
I'm assuming that it's using root and that's the user I built it with. I will look into seeing if it's anybody else, just taking a quick look though I don't see anything else that could be using it. I only copied the egg file to the plugins folder, I set up another folder elsewhere and built it and cp to the plugins folder. I'm glad to know I was doing that right because looking up documentation on how to restart trac turns up practically nothing, they just say restart trac or restart apache. I will look into the logs later on tomorrow. Thanks for the replies! Also we are using trac .12.1.
So after looking at the log files it seems that it doesn't even load the plugin, can't find anywhere that says it's loading or any errors with it. Now we have a few trac sites for various projects and one of the sites already has plugins installed so I went there and and put the test plugin there and checked logs and it didn't work either. So I'm just going to conclude it's the plugin or something we already have in place and it's not me. I believe I'm going to try and make one and test it. Thanks for the help!
It sounds like you built the egg correctly. After you copy it into your plugins folder, change the file's owner and group (I'm assuming you're on Linux since you mentioned chmod) to match the account that your webserver uses. I'm not sure if that's strictly necessary, but it's what's always worked for me.
I may be misreading your #4, but it sounds like you copied the whole egg folder to your plugins directory. Only the .egg file needs to be copied over, it's a self-contained package. I don't think Trac looks for .egg files in subdirectories.
Restarting your webserver is the easiest way to restart Trac. Actually, I'm not aware of any other way to do it.
When it comes to plugin problems, Trac's log is usually a very good source of information. I recommend setting Trac's log level to DEBUG, then shut down the web server. Clear out the contents of Trac's log file, then start the web server and make a copy of Trac's log file after the server has completely come back online. Do this process twice: once with the plugin installed and once without it installed. The difference in the logfiles should give you a good indication of what the problem is. Once you get accustomed to what your logs normally look like, you'll be able to read the log in place without clearing it out and generating two versions of it.
By the way, what Trac version are you using?
Check the Trac version and downloaded plugin
instead python setup.py bdist_egg try python setup.py install
Quite an old thread, but since I ran into the same problem at one point:
Make sure you build the .egg with the same Python version that you use to run Trac with!Backwards compatibility between Python versions does not matter here, because Trac reads information about the Python version out of the .egg file before it even loads it, to make sure it is compatible.
(Small version numbers should not matter, so you should be able to run a .egg with Python 2.7.10 when it was built with 2.7.3, but not when it was built with 2.6.x. Look at the Version number that is written into the .egg file name.)

On the google app engine, why does my 'import' statement fail on Live, but work on Dev(localmachine)?

I have a python/django application that runs on the google app engine.
My views.py file has some imports...
from commands.userCommands import RegisterUserCommand
from commands.accountCommands import CreateNewAccountCommand, RenameAccountCommand
These imports work fine on my development environment (local machine). But when I upload to the google app engine, views.py fails with a "Could not import views. Error was: No module named userCommands" error.
Any idea why I can't import my commands.userCommands module?
My file structure looks as follows...
- app.yaml
- urls.py
- views.py
- etc...
- commands/__init__.py
- commands/userCommands.py
Note: I did try to append my application name to the module name/path. No luck.
Note: I did do an update with the --noisy argument, and it does appear to upload my commands folder successfully.
You could be running into a clash with Python's own commands module (which doesn't have submodules like yours) -- naming your own modules and packages in ways that are meant to hide ones in the standard library (just like naming your variables in ways that are meant to hide builtin names, like list or file) is always a perilous undertaking, even though it "should" work there's always potential for confusion.
Could you try renaming that commands package and its uses to something unambiguous and free from danger, such as mycommands, and see if that just makes the problem disappear? If that's the case you can then open a ticket on GAE's tracker (because it would show a minor but undeniable bug in GAE's runtime) but meanwhile your problem is solved!-) If the problem stays, ah well, at least we've eliminated one likely cause and can keep digging...
The __init__.py files are required to make Python treat the directories as containing packages, so you need a
commands/__init__.py
file in your directory structure. See http://docs.python.org/tutorial/modules.html.

Categories

Resources