Module not found error when im creating my own pypi package - python

Im creating my own simple package that i will upload on pypi that generates jokes on my native language. While creating the package I'm bugged with this error : ModuleNotFoundError: No module named 'utils'
Here is my folder structure :
Myjokes/
myjokes/
data/
jokes.json
utils/
helpers.py
__init__.py
jokes.py
when i am in the Myjokes/ directory and i run the python -c "from myjokes import jokes" it runs but when i run the functions inside the jokes file it displays a module not found error : from utils.helpers import get_jokes_by_count , get_joke_by_dialect , pinoy_jokes ModuleNotFoundError: No module named 'utils'
if i go inside myjokes/ directory which is the subdirectory of Myjokes i can run the jokes.py script via import jokes and everything is running. i dont know why this is happenin I'm fairly new to python.

Python doesn't know that your utils/ folder is supposed to be treated like a python package unless you include an __init__.py in that folder as well.
Try adding a blank __init__.py file inside utils/ and see if that works for you.

Related

ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package

I have a Django project with the following structure:
project
apps
news
__init__.py
models.py
hose
tasks.py
Within tasks.py, I have this import
from apps.news.models import Company
When I try to run tasks.py, I get the following error:
ModuleNotFoundError: No module named 'apps.news'; 'apps' is not a package
How do I fix this error? I don't understand why this import does not work.
Reference: https://docs.python.org/2/tutorial/modules.html#packages
You need to add an empty __init__.py (4 underscores in total) file in the apps folder for it to be recognized by Python as a package.
In case anybody else is still struggling with this. I had the __init__.py in the app folder but it still didn't work. Running the following command in app parent folder worked for me.
export PYTHONPATH=$PWD

How to import from a python package which is in a sibling directory?

I am new to python and i am having some issues with packages and imports.
My structure is as follows:
src/
base/
packcore/
__init__.py
utils/
__init__.py
util_1.py
util_2.py
helpers/
__init__.py
helper_1.py
helper_2.py
some_script.py
app.py
packcore is an external package that has been installed using pip and put into the --target=base.
some of the helpers in the packcore uses some of the utils.
From app.py i want to be able to import a helper/util.
But when i use from base.packcore.utils import some_util i get an error saying that no module named packcore from inside the helper/util
and if i do from packcore.utils import some_util i get an error no module named packcorefrom theapp.py`
help would be much appreciated :)
If you add an __init__.py to base/, you can make it a Python package to import from. You also need to make the parent a package (Which is currently called src) so it is actually a sibling module, rather than many isolated modules.
From there, you can either do an absolute import from the main package:
from src.base.packcore.helpers import helper_1
Or relative (Assuming you are in some_script.py or app.py):
from .base.packcore.helpers import helper_1

Django: no module named http_utils

I created a new utils package and an http_utils file with some decorators and HTTP utility functions in there. I imported them wherever I am using them and the IDE reports no errors, and I also added the utils module to the INSTALLED_APPS list.
However, when launching the server I am getting an import error:
ImportError: No module named http_utils
What am I missing? What else do I need to do to register a new module?
Make sure the package is correct (Include init.py file).
Make sure there are no other utils files in the same directory level. That is if you are importing from utils import http_utils from views.py, there should not be a utils.py in the same folder. Conflict occurs because of that.
You dont have to include the folder in the INSTALLED_APP settings. Because the utils folder is a package and should be available for importing
As stared by Arundas above, since there is a utils.py file I suggest renaming the module to something such as utilities, and also make sure you have a __init__.py file in that directory.
from utilities.http_utils import class_name

Python - ImportError

I have a module I have installed called lts_fits, and this is its path:
~/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/lts_fits
So it is clearly in the site packages folder. Within this folder, there is a python script:
lts_linefit.py
Yet when I have this line of code in my script:
from lts_fits import lts_linefit
I get this error:
ImportError: No module named lts_fits
How? It's clearly in there, and I have tried this same syntax with other random scripts and they import just fine. For instance, a file abc.py located in the folder ~/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sympy imports just fine when I have the line from sympy import abc. What could be going wrong?
You need an __init__.py file in that directory (you do not have to put anything into the file, all you need to do is create it).
The easiest way to create said file is by using:
touch __init__.py
from within your lts_fits directory in your command line/terminal/console.
See this SO article: What is __init__.py for?
And the Python Documentation for packages.

ImportError: No module named test.meetinghandler

Hey friends i set my python path as
/home/rohit/test
Here is the structure my test folder
test/
__init__.py
meetinghandler/
__init__.py
meetinghandler.py
db/
__init__.py
models.py
setting.py
manage.py
My problem is when i am trying to import
from test.meetinghandler import meetinghandler
from models.py
i am getting error i.e;
ImportError: No module named test.meetinghandler
please help me out what i am doing wrong .
from test import meetinghandler looks for a test module, which it will not find in your python path.
The reason? You added /home/rohit/test/ to your python path, but that directory doesn't itself contain a test directory :-) Python looks for modules inside the path, that's why.
In your case you'd have to add your home dir to your python path to be able to find it. (Or better, google for virtualenv and so).
Try debugging from the console.
See if the import test works without error. If so, likely there is some error statement in your meetinghandler.py that is not loding the file.
If the import test itself fails on the console, see if the test is in the python path (i.e, as a part of the folder that is present in the settings.INSTALLED_APPS)

Categories