AttributeError: module 'MyModule' has no attribute 'Module1' - python

I am trying to turn a project of mine into a package so I can deploy it as a wheel.
I have a project directory setup like this:
ProjectDir
├── setup.py
├── MyModule
│ ├── __init__.py
│ ├── Module1
│ │ ├── __init__.py
│ │ ├── main.py
│ ├── Module2
│ │ ├── file1.py
│ │ ├── __init__.py
│ │ ├── file2.py
│ │ ├── file3.py
│ └── Module3
│ ├── __init__.py
│ ├── Sub1
│ │ ├── file1.py
│ │ ├── file2.py
│ │ ├── main.py
│ └── Sub2
│ ├── file1.py
│ ├── main.py
└── test
├── test_Module_1
│ ├── __init__.py
│ └── test_main.py
├── test_Module_2
...
Top level __init__.py is empty
Module 1 __init__.py file
from main import Function1
Similar for other module __init__.py files
setup.py
from setuptools import setup, find_packages
import os
import pip
setup(name='MyModule',
description='Tool suite to run MyModule',
packages=['MyModule'])
I can import MyModule but when I attempt to access any submodule, I get the following
AttributeError: module 'MyModule' has no attribute 'Module1'
Or if I check attributes of my module, none are found.
import MyModule
dir(MyModule)
['builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'os']

This is expected because by default submodules are not imported.
You should import them like so to use them:
import MyModule.Module1
To change this you have to tweak the MyModule/__init__.py file by adding:
import MyModule.Module1
This way, MyModule.Module1 will be available when you import MyModule, as the __init__.py file is executed.

Related

ImportError attempted relative import beyond top-level package

This is my project structure:
.
├── connectapp
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── djangoproject
│ ├── __init__.py
│ ├── __pycache__
│ ├── asgi.py
│ ├── djangoproject.sqlite3
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── djangoproject.sqlite3
├── hackerapp
│ ├── Controllers
│ ├── Models
│ ├── Serializers
│ ├── __init__.py
│ ├── __pycache__
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ └── tests.py
├── manage.py
└── requirments.txt
When I do below being in top level folder:
python3 manage.py migrate hackerapp
I am getting this:
djangoproject/djangoproject/urls.py", line 19, in <module>
from ..hackerapp.Controllers import PersonViewSet, DepartmentViewSet
ImportError: attempted relative import beyond top-level package
To me looks import should work but it's not, can someone tell me why?
I guess (?) migrate hackerapp are cli params to the manage.py script. The error means that the top level folder is not a package. To make it into a package add an (empty) __init__.py file then from that folder run:
python -m manage migrate hackerapp # note no .py

ImportError: No module named <module name> for local module imports in Python

I am very new to Python and I have the following structure for a project:
server/
├── config/
│ ├── __init__.py
│ ├── application.py
│ ├── dev.py
│ └── qa.py
├── lib/
│ ├── __init__.py
│ ├── redisdb.py
│ ├── logger.py
│ └── geo.py
└── scripts/
├── __init__.py
├── my_first_script.py
└── my_second_script.py
and in my_first_script.py file, I have the following code:
import pickle
from lib.redisdb import r
import re
import config.application as appconf
print( appconf.DOCUMENT_ENDPOINT )
partnerData = pickle.loads(r.get("partner_date_all"))
print( len(partnerData) )
When I run this code in the terminal using the command
python server/scripts/my_first_script.py
I am getting the following error:
Traceback (most recent call last):
File "my_first_script.py", line 3, in <module>
from lib.redisdb import r
ImportError: No module named lib.redisdb
I am using Python 2.7 version here. When I checked with Python 3 also, I have the same error. Now how can I execute this file? If my code doesn't have imports from the other local modules, everything works just fine.
Your modules are all siblings and you didn't not declare a parent package.
You could modify your structure this way so your modules can know each other.
server/ (assuming this is your project root)
server/ (assuming you want to call your package "server")
├── __init__.py
├── server.py (your package entry point)
├── config/
│ ├── __init__.py
│ ├── application.py
│ ├── dev.py
│ └── qa.py
├── lib/
│ ├── __init__.py
│ ├── redisdb.py
│ ├── logger.py
│ └── geo.py
└── scripts/
├── __init__.py
├── my_first_script.py
└── my_second_script.py
And now your imports can refer to the parent package:
for example:
# my_first_script.py
from server.config import application

Python finding subdirectories with pkgutil's extend_path

This is my folder structure
src
├── __init__.py
└── foo
├── __init__.py
└── bar
├── __init__.py
├── events
│ ├── __init__.py
│ ├── execute
│ │ ├── __init__.py
│ │ ├── helloworld.py
│ │ ├── run.py
└── settings.py
$ cat src/foo/__init__.py outputs...
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
in src/foo/bar/events/execute/run.py, I want to do something like this
from foo.bar.events.execute.helloworld import HelloWorld
I get the error
No module named 'foo.bar'
This is how I'm running my app
I understand it's not proper, but there's a reason I just simplified the question for brevity
$ python src/foo/bar/events/execute/run
How do I achieve importing this way from src/foo/bar/events/execute/run.py?
from foo.bar.events.execute.helloworld import HelloWorld

Cannot import model from .models file to nested app sub_directory

I suspect that I am not understanding something about django. I am trying to test the file scrape.py which calls an api and parses then writes the response to the model CMC in the models.py using sqlalchemy. I am trying to test it to see if the file itself will run but (here's where I think I'm going wrong) I am pressing the play button while having the scrape.py file pulled up in vscode. I suspect there is something here that Django won't allow but am not familiar enough with django to know if that is the case. It is throwing a ModuleNotFound error as described below but I suspect that it's doing that because I am calling it because when I type the dot after apis after import at the top of the file it shows a list of all the drop-down files in my apis app.
Here is my project tree:
(base) justinbenfit#MacBook-Pro-3 cds_website % tree
.
├── api
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── admin.cpython-38.pyc
│ │ ├── apps.cpython-38.pyc
│ │ ├── models.cpython-38.pyc
│ │ ├── serializers.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── views.cpython-38.pyc
│ ├── admin.py
│ ├── apps.py
│ ├── main.py
│ ├── management
│ │ ├── __init__.py
│ │ ├── commands
│ │ │ ├── __init__.py
│ │ │ ├── __pycache__
│ │ │ │ └── private.cpython-39.pyc
│ │ │ ├── private.py
│ │ │ └── scrape.py
│ │ └── test.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ │ ├── 0001_initial.cpython-38.pyc
│ │ └── __init__.cpython-38.pyc
│ ├── models.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── cds_website
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ ├── settings.cpython-38.pyc
│ │ ├── urls.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── requirements.txt
api is an app in a greater project called cds_website. The settings.py file in cds_website project directory contains the following installed apps:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api.apps.ApiConfig',
'rest_framework',
'environ'
]
and my apps.py file in the api app contains the following class:
from django.apps import AppConfig
class ApiConfig(AppConfig):
name = 'api'
CMC is a model in my models.py file. I am trying to import it into scrape.py. I have tried:
from ...models import CMC
from api.models import CMC
from ... import CMC
first one throws: ImportError: attempted relative import with no known parent package second one throws: ModuleNotFoundError: No module named 'api' third one throws: ImportError: attempted relative import with no known parent package
Here is a link to the code repo: https://github.com/Justinbenfit23/cds_website
Everything I have read indicates that at least one of these should work. Any direction appreciated!
I'm teste your project with from api.models import CMC and it's worked
The easiest way is to create ecrape_manage.py in cds_website-master
folder, content of ecrape_manage.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cds_website.settings")
import django
django.setup()
exec(open("api/management/commands/scrape.py").read())
and just run with python ecrape_manage.py

python import from different level directory no module find

python import from different level directory getting import error
Directory structure.
# all __init__.py files are empty
import/
├── helpers
│ ├── __init__.py
│ ├── helper1
│ │ ├── __init__.py
│ │ ├── helper1_module1.py
│ │ └── helper1_module2.py
│ └── helper2
│ ├── __init__.py
│ ├── helper2_module1.py
│ └── helper2_module2.py
└── services
├── __init__.py
├── service1
│ ├── __init__.py
│ └── service1_api.py
└── service2
helper1_module1.py
class Helper1Module1():
def function1(self):
print("executed from Helper1Module1 Class function1")
def function2(self):
print("executed from Helper1Module1 Class function2")
service1_api.py
from helpers.helper1.helper1_module1 import Helper1Module1
h = Helper1Module1()
h.function1()
Error:
python3 services/service1/service1_api.py
Traceback (most recent call last):
File "services/service1/service1_api.py", line 1, in <module>
from helpers.helper1.helper1_module1 import Helper1Module1
ModuleNotFoundError: No module named 'helpers'
How to fix this?
Python: python3.6 and above
OS: Linux
You have to set the file path (PYTHONPATH) manually to use the files from other directories.
You can export Environment Variable like
export PYTHONPATH=’path/to/directory’
Or you can use sys module: Importing files from different folder

Categories

Resources