Won't import local file as module - python

I'm setting up a Flask application on Digitalocean and have Python 3.7 installed and the latest version of Flask. When running the app inside a virtualenv and trying to run the application using python3.7 application.py I get the following error message:
Traceback (most recent call last):
File "application.py", line 11, in <module>
from config import *
ModuleNotFoundError: No module named 'config'
What puzzles me is that config.py is located in the same folder as application.py, and not in a subfolder. I have duplicated the setup on my local machine, also running Python 3.7 and inside a virtualenv, and the importing (and the app) works flawlessly.
I've tried importing "config.py" instead of just "config" but didn't make a difference. I also tried specifying exactly what it should import (instead of using '*') but that didn't make a difference either.
Your thoughts on why it can't find config?

What seems to have been the solution to my problem above was to run the Python shell and add the path to the directory in which config.py is located (even though it's in the same folder as application.py...) by using the following command:
sys.path.append("/path/to/config/")

Related

IIS CGI could not locate my python modules

I am trying to run my python scripts on my IIS website using CGI and I am having trouble with importing. When run on its own, the python scripts finds the mysql.connector module installed in my os perfectly fine, but when I try to run it on the website, it gives a Bad Gateway (502.2) error with the stacktrace stating ModuleNotFoundError: No module named 'mysql.connector'. I'm assuming CGI cannot find the module in my OS, how can I let it find the module? Do I have to specify my modules folder somewhere in the IIS like a PATH variable?
Here is the complete stacktrace of the bad gateway page:
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "Traceback (most recent call last): File "C:\Users\pedro\OneDrive\Documents\adet\ind.py", line 2, in import python_mysql File "C:\Users\pedro\OneDrive\Documents\adet\python_mysql.py", line 1, in import mysql.connector ModuleNotFoundError: No module named 'mysql.connector' ".
As a workaround, I simply copied and pasted all of my modules in my modules folder to my website's folder. I guessed that since my personal imports were working that it should also work if I added the other modules to the same folder and lo, it did. I hope there's a cleaner way to solve this, but for now I'll choose this as a solution.

ModuleNotFoundError: No module named in Python

I keep getting this error in VS Code:
Traceback (most recent call last):
File "c:\Users\User Name\Documents\Productivity\Coding\Python\Udemy\Projects from course\MilestoneP2\app.py", line 1, in <module>
import MilestoneP2.utils.Operations_db as Db
ModuleNotFoundError: No module named 'MilestoneP2'
I have recently shifted from Pycharm to VS Code and I am trying to open some of those projects from pycharm in VS Code but there is the above error haunting me.
PS: I have my Python Interpreter in a different directory and not in the workspace folder. It's in D:\Python\venv Drive
Here is my code:
import MilestoneP2.utils.Operations_db as Db
Here is the file hierarchy.
Anyone Help?
Thank you
If you run the script within VSCode there is a Python version button on bottom-left.
When you click on it you can specify your Python or virtual environment path. It will also try to find them automatically from directories. Then you can run your scripts with the spesified environments.

How can I permanently add a module to my flask app?

yes I know my question sounds like a duplicate but I have tried everything I have found.
I am trying to add a module to my python flask app. This module is located at ../../clients/api relative to my flask app. I haven't had any problems adding a module before, but for some reason this just doesn't want to work.
So far, I have tried:
sys.path.append("mypath") (worked temporarily but as soon as the shell was closed it disappeared)
adding a .pth file in lib/site-packages
adding it directly to my environment variables
adding in in my IDE (Wing 101)
Aside from 1., none of them had any effect whatsoever.
$ flask run
* Serving Flask app "main.py"
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: While importing "main", an ImportError was raised:
Traceback (most recent call last):
File "c:\python37-32\lib\site-packages\flask\cli.py", line 235, in locate_app
__import__(module_name)
File "my_flask_app_path", line 20, in <module>
from clients.api import Client as client
ModuleNotFoundError: No module named 'api'
Running this on python v-3.7.1, Windows 10, both git bash and cmd. Please try not to be blunt :)

Virtual environment python causing Django.core not found error

I am currently experiencing problem with the virtual environment.
Using virtual environment wrapper, I used 'workon' to be in that environment. In this enviornment there is a Python 2.7 in Scripts folder and when I run python, it is indeed running this python.
When I run django-admin.py it returns the following error:
Traceback (most recent call last):
File "C:\Users\JOHN\Envs\courseva\Scripts\django-admin.py", line 2, in <module>
from django.core import management
ImportError: No module named django.core
FYI, I installed django and it is within this Envs/courseva folder (Lib/site-packages folder)
now. I was curious so I ran python and in the command I tried 'from django.core import management' and it is not throwing an error and I am really confused now.
I was concerned with the default Python 2.7 in C: so I even changed the environment path but no use. Please help.

Python client library for Fusion Tables: Running oauth_example.py answers "No module named authorization.oauth"

i've installed python client library for Fusion Tables (here) with all dependencies. But if i try to run oauth_example.py, it aswers me
Traceback (most recent call last): File "./oauth_example.py", line
13, in
from authorization.oauth import OAuth ImportError: No module named authorization.oauth
my python version is 2.7.2+ running on Ubuntu 11.10
and yolk -l
oauth2 - 1.5.211 - active development (/usr/local/lib/python2.7/dist-packages/oauth2-1.5.211-py2.7.egg)
What's wrong?
It seems like you are running the script from the src/samples directory. Try copying the script up one directory and run from the src directory where it can find the authorization/oauth.py module. Or you could include something like this in the top of your python script:
import sys
sys.path.append( '/home/src/python_modules/fusion-tables-client/src')

Categories