ModuleNotFoundError even though __init.py__ exists - python

I have a discord bot written with python. But the catch is, it only works when deployed on heroku but doesnot run locally at all.
This is the folder structure
- feed
- __init__.py
- token.py
- main.py
-requirements.txt
When I run the command python3.9 feed/main.py, it gives the following error:
from feed import token
ModuleNotFoundError: No module named 'feed'
What is the issue here? Mind you that the code runs without any errors on heroku with the same command. I am on Ubuntu 21.04.
If I change feed to .feed, I get other errors regarding absolute imports.
Please don't redirect me to other answers, I have tried it all.

I don't know how Heroku works, but to get it running locally, try changing your import to:
import token
If you want to use feed as the parent directory, you can create a setup.py file and run pip install -e . in the folder. Then doing
from feed import token
should work as part of your development environment.

Related

Google Cloud Buildpack custom source directory for Python app

I am experimenting with Google Cloud Platform buildpacks, specifically for Python. I started with the Sample Functions Framework Python example app, and got that running locally, with commands:
pack build --builder=gcr.io/buildpacks/builder sample-functions-framework-python
docker run -it -ePORT=8080 -p8080:8080 sample-functions-framework-python
Great, let's see if I can apply this concept on a legacy project (Python 3.7 if that matters).
The legacy project has a structure similar to:
.gitignore
source/
main.py
lib
helper.py
requirements.txt
tests/
<test files here>
The Dockerfile that came with this project packaged the source directory contents without the "source" directory, like this:
COPY lib/ /app/lib
COPY main.py /app
WORKDIR /app
... rest of Dockerfile here ...
Is there a way to package just the contents of the source directory using the buildpack?
I tried to add this config to the project.toml file:
[[build.env]]
name = "GOOGLE_FUNCTION_SOURCE"
value = "./source/main.py"
But the Python modules/imports aren't set up correctly for that, as I get this error:
File "/workspace/source/main.py", line 2, in <module>
from source.lib.helper import mymethod
ModuleNotFoundError: No module named 'source'
Putting both main.py and /lib into the project root dir would make this work, but I'm wondering if there is a better way.
Related question, is there a way to see what project files are being copied into the image by the buildpack? I tried using verbose logging but didn't see anything useful.
Update:
The python module error:
File "/workspace/source/main.py", line 2, in <module>
from source.lib.helper import mymethod
ModuleNotFoundError: No module named 'source'
was happening because I moved the lib dir into source in my test project, and when I did this, Intellij updated the import statement in main.py without me catching it. I fixed the import, then applied the solution listed below and it worked.
I had been searching the buildpack and Google cloud function documentation, but I discovered the option I need on the pack build documentation page: option --path.
This command only captures the source directory contents:
pack build --builder=gcr.io/buildpacks/builder --path source sample-functions-framework-python
If changing the path, the project.toml descriptor needs to be in that directory too (or specify with --descriptor on command line).

Python ModuleNotFoundError for Custom Module

I am importing a custom module called as Helper to my unittest. When I run with the help of test explorer it works fine, but when I try to run through command line it give me below error. ModuleNotFoundError: No module named 'Helper'
I have manually added empty init.py file at each directory and at project level as well.
My python version is : 3.8.3
Below is the error I get in CMD.
error while running through command line
Here is my folder structure ,result and code when I execute through test explorer.
sampleTest.py code
globalHelper.py
globalHelper.py code
first of no need for empty __init__.py files in those other directories. Now to import a module that is in a directory use:
from dir_name.module_name import what_you_need
hope I did not mess up some terms and that you understood what I meant

Installing a flask application in development mode

I have followed flask's tutorial to the point of installing the flaskr blog (here: http://flask.pocoo.org/docs/1.0/tutorial/install/). I'm new to installing python packages and I would appreciate some explanation of what's going on:
The tutorial says that installing the flaskr project has the benefit of being able to run it from anywhere.
However, I still can't run it outside the flask-tutorial directory (if I do flask run outside the flask-tutorial directory in the command line, I get flask.cli.NoAppException: Could not import "flaskr".)
Also, while I can import flaskr when working in the python interpreter, I also can't run it from there (even when I'm in the flask-tutorial directory):
import flaskr
app = flaskr.create_app()
app.run()
I get the following, and the python interpreter exits:
* Serving Flask app "flaskr" (lazy loading)
* Environment: development
* Debug mode: on
* Restarting with stat
/Users/user/projects/flask-tutorial/venv/scripts/python.exe can't find '__main__' module in ''
--- UPDATE:
If I set $env:FLASK_ENV = "production", and then start the python interpreter, I can run flaskr successfully as described above.
However, still no luck running flaskr directly from the command line (with flask run) outside of the flask-tutorial directory.
You are unable to use it outside the project folder because you have installed it in editable mode (-e flag). Uninstall the package and install again without the -e switch, so that it will be installed globally on your system. After that, it should work.

importing from another folder in virtualenv

I'm following the Flask Mega Tutorial, and I'm running into an issue once I get to the second part and restructure my folder structure to match theirs, I cannot import Flask.
My current folder structure is as follows
/FlaskTest
/app
/static, templates etc
/flask
/virtualenv folders etc
/tmp
run.py
as far as I can tell, the folder structures are identical other than naming of the top level directory.
in my __init__.py file (/app/__init__.py), I'm doing as instructed in the tutorial,
from flask import Flask
app = Flask(__name__)
from app import views
I'm getting an Import Error that "cannot import name 'Flask'". I'm guessing the issue is because the flask package was installed to /flask/lib/site-packages.
My question: How can I reference the sub folder of flask/site-packages?
I've read through the python import system documentation and from what I can make of it through the first pass of reading it over, I would need to likely do something like from flask import flask.Flask or something to that effect.
UPDATE: So after cd'ing around the directory and checking pip list, I realized that flask wasn't accessible to my app directory. I ran pip install flask in the app directory. Now my site runs, but I'm not sure if this is the best practice of doing things with Python. Please provide some clarity as what the best practice is for installing packages and where the packages reside.
UPDATE 2: After creating a directory called standalone. In this folder, I created a virtual environment called standalone-test. Once, I did that, I also mkdir'ed app and copied it's contents from FlaskTest so that way the code would be identical. I was able to run the run.py script by using python run.py, but I can't run python -m app like you had said without running into an error. The error is as follows if it helps.
"No module name app.main; 'app' is a package and cannot be directly executed.
I am able to run python run.py as I mentioned, but I'm not able to run the python -m app command as you had mentioned
I think something went wrong in your execution environment. Here are some explanations.
The virtualenv
See the documentation of virtualenv
If you have followed the tutorial:
The flask directory is your virtualenv,
On posix system, you have a flask/bin subdirectory, or
On Windows system, you have a flask\Scripts subdirectory.
I make the assumption that you are on posix system.
To activate your virtualenv, run:
source flask/bin/activate
Your prompt should change to something like: (flask)$.
To list the installed libraries use pip:
pip list
Make sure you see Flask. The tutorial encourages you to install a lot of Flask plugins, so there are a lot of Flask-Something…
If Flask is missing, install it:
pip install Flask
Run your app
Your application is in the app directory, it has an __init__.py file (it's a Python package).
In this file, you have:
from flask import Flask
app = Flask(__name__)
from app import views
From your FlaskTest/ directory, try to run this script like this:
cd FlaskTest/ # if not in this directory
python -m app
This should import Flask, instanciate your app (but don't run it), import the views module.
If app/views.py exist you should have no error.
=> at this point, we have simulated what run.py imports…
Now write run.py in your FlaskTest/ directory:
#!flask/bin/python
from app import app
app.run(debug=True)
Run it like this:
python run.py
Note that the shebang #!flask/bin/python is unusual, but should work in the context of the tutorial.
This should start your http server…

Importing a module outside of Jenkins home directory

I am trying to run a custom Python module located at '/home/modules/module.py'
The python script is run by Jenkins and located at '/home/user_name/scripts/script.py'
Since Jenkins home directory is '/var/lib/jenkins' how can I import my module?
I have tried adding the following to the python script:
import sys
sys.path.insert(0,'/home/modules') """also /home/modules/"""
import module
but I am still getting the error:
ImportError: No module named
I am running everything in Ubuntu 14.04 and Python 2.7
EDIT:
I changed Jenkins user directory like this:
root#dwh-01:~$ usermod -m -d /home/jenkins jenkins
and modified JENKINS_HOME=/home/jenkins in /etc/default/jenkins
I think I can work with this, but now the issue is, if I log into jenkins shell and do:
jenkins#dwh-01:/$ cd
bash: cd: jenkins/: No such file or directory
Should the behaviour of the cd command direct me to /home/jenkins/ ?
If I repeat the same when in /home/ It works though.
If I try and start jenkins, it gives me:
Starting Jenkins Continuous Integration Server jenkins
No directory, logging in with HOME=/
EDIT-2:
User jenkins home dir error fixed, I just made sure the home directory of the user was /home/jenkins using usermod -d /home/jenkins jenkins
I'm a step closer to importing the module, but am still having issues going one step outside of the jenkins home directory.
You should have two options that would allow you to import module into your code.
Option 1
import sys
sys.path.append('/home/modules')
import module
Option 2
import importlib.machinery
importlib.machinery.SourceFileLoader('module', '/home/modules').load_module()
If neither of these work, hopefully they will at least point you and others in the right direction.
Ok finally figured it out.
What I did was change in the /etc/default/jenkins file the variable JENKINS_USER and JENKINS_GROUP to the user i needed to outside of the jenkins user folder (outside of /var/lib/jenkins).
This way the scripts ran by Jenkins will be ran as if it was the selected user I specified.
After that, I realized that in Jenkins, even if the working directory is /var/lib/jenkins/jobs/adjust_data_parser/workspace, scripts and files can be called from '/'
So the trick was:
-Accessing the script outside of the jenkins home directory
-Importing the module from its absolute path.

Categories