Apologies if this is a duplicate question. I tried searching up my question but didn't find anything.
I have a Django Project with 3 Django Apps and the project directory looks like this
Project
|__ App_1
|__ App_2
|__ App_3
|__ views.py
|__ models.py
|__ scripts
|__ class1.py
The scripts folder has python scripts that run algorithms using data from the database. I am using the Django Queryset to retrieve data. The class1.py file would look something like this:
from App_1.models import table1
.
.
.
When doing that, I get this error:
ModuleNotFoundError: No module named 'App_1'
Curious, I moved class1.py to the App_1 folder and I got this error.
ImportError: attempted relative import with no known parent package
I am a bit confused as to why BOTH methods did not work. What would be the best way to go about this? Ideally, I want to setup my scripts so I can just declare an object in a view-based function in App_1/views.py
Related
I have this file structure in my python project
|__src
|__main.py
|__gen.py
|__app
|__ __init__.py
|__ app.py
|__ lang.py
Intention
I want to use the Language class from sibling module lang.
So I tried with this import statement in app.py:
from app.lang import Language
Issue
But when I run app.py I get a ModuleNotFoundError error saying 'app' is not a package:
Which doesn't make sense since app has __init__.py.
How can I solve this?
Because both app.py and lang.py are in the same directory try to import like this :
from .lang import Language
or you can use from app.lang import Language from another file located outside app folder
I am currently working on a Flask application. I would like to organize my .py files in different folders. This is a three that can describe all the folders.
Application
|_Interpreter
|_
__init__.py
parser.py
|_ Classes
|_ Interfaces
__init__.py
Expressions.py
|_ Operations
__init__.py
Arithmetic.py
Operations.py
|_ SymbolTable
__init__.py
Symbol.py
Symbol_table.py
__init__.py
Driver.py
From my knowledge, the __init__.py makes the folder a module, then you should be able to import them from another files.
For example, from the file Operations.py I want to import Expressions.py. This is how I would do it:
from Classes.Interfaces.Expression import Expression The issue is that I keep getting a ModuleNotFoundError: No module named 'Classes' I've tried to use
import sys
sys.path.append('\..')
But it doesn't work all the times, in the example given it doesn't work.
I know that I can append the path of the file in my computer, but I think that it will produce some issues if I want to deploy an application to a server. Which is the best option to import modules?
Have you tried adding an __init__.py to you Application folder?
You might want to use from Application.Classes.Interfaces.Expression import Expression then.
I'm having a similar but different issue from the other posts I've seen on here: I'm trying to import a module from a nested module, but even though the python linter picks up everything fine I can't execute the file because of an import error, module not found.
ParentFolder /
|__ContainerFolder
|__ __init__.py
|__ Camera/
|__ __init__.py
|__ CameraService.py
|__ Data.py
|__ Settings /
|__ __init__.py
|__ SettingService.py
|__ Handler /
|__ __init__.py
|__ Handle.py
|__ Models /
|__ __init__.py
|__ Setting.py
What I want is to import Data.py inside of Handle.py
I have tried:
from ContainerFolder.Camera.Data import DataClass
The linter says it's fine, and the autofill in VS Code gives me type-ahead, however, at execution I get a ModuleNotFoundError for ContainerFolder module. I have an __init__.py in all the directories so what am I missing to make that a module to import from?
edit
So CameraService.py and SettingService.py are both Tornado APIs, since they are both executing as main how would one be able to share modules between the two? ie Data.py with modules under the Settings directory or Setting.py within modules under the Camera directory?
Try converting the file into a package like here
https://www.jetbrains.com/help/pycharm/refactoring-convert.html
or here
Converting a python package into a single importable file
I have an app structured like so:
api/
|_app/
|_ __init__.py
|_conf/
|_resources/
|_ __init__.py
|_controller.py
|_dao/
|_ __init__.py
|_thing_dao.py
|_etc...
I want to use the function get_thing_by_id in thing_dao.py inside of controller.py. In PyCharm I start to type get_thing_by_id and it prompts me to auto-import the function. The problem is it simply does from thing_dao import get_thing_by_id but when I attempt to run the app (Flask) I get ImportError: No module named 'thing_dao'. What I end up having to do is a relative import or start at the module level from app.dao.thing_dao import get_thing_by_id.
I suspect this issue is related to my project structure and less of a PyCharm bug. Is there a way I could structure my project to better hint to PyCharm how to do imports? I've looked through the configuration option for auto-imports and PyCharm and they're quite slim so I suspect that I'm doing something wrong.
I discovered I had my directories marked as a "Source Root". Unmarking them made the imports work properly.
I have been trying to create a Google App Engine project that contains various modules, each with various versions; yet I cannot get it to work.
I went through everything on the google modules guide website but their description of the hierarchy of the application is very vague. I downloaded a simple test application to see how it works (which I cannot get to work).
This is the structure of the application right now:
http://www.iteratorium.eu/stackoverflow/structure.jpg
and the whole thing is zipped up here:
http://www.iteratorium.eu/stackoverflow/flask_app.zip
There are three modules: default, my-module and mobile-frontend, and both the my-module and mobile-frontend have two versions in directories v_one and v_two. Each version has its own .yaml file, which contains a single handler (keeping it simple for testing purposes)
Is the structure at least correct? When I load the application through the dev_appserver.py file, everything is fine as long as I do not load two versions of the same module. In that case, this happens:
me#MY_COMPUTER:~/flask_app$ python ~/google_appengine/dev_appserver.py dispatch.yaml app.yaml mobile-frontend/v_one/mobile-frontend.yaml my-module/v_one/my-module.yaml my-module/v_two/my-module.yaml
Results in a traceback and the following:
google.appengine.tools.devappserver2.errors.InvalidAppConfigError: Duplicate module: my-module
(Both versions get uploaded onto appengine via appcfg.py without any errors, it might be only the localhost server cannot handle many versions)
If I only load one version of each module, everything works, but the modules cannot import anything from the lib directory.
Accessing http://localhost:8082/mobiler through the browser results in this in the terminal:
from flask import Flask
ImportError: No module named flask
I defined the path to the lib folder in the appengine_config.py file but it does not seem to work for the modules. That is where I stopped and decided to come here, since I am not even certain the structure is correct and I might be way off with all of this.
So... How far off am I?
note: The dev server won't let you load different versions of the same module
You need the module definitions to be in the toplevel dir for appengine_config.py to get loaded.
Considering you really really want to keep the code for the two versions separated, a better app organization would be as follows (just showing mobile-frontend to keep it short):
root
|__ mobile-frontend
| |__ v_one
| |__ __init__.py
| |__ mobiler.py
| |__ v_two
| |__ __init__.py
| |__ mobiler.py
|__ appengine_config.py
|__ dispatch.yaml
|__ mobile_frontend_v_one.yaml
|__ mobile_frontend_v_two.yaml
And having mobile_frontend_v_one.yaml being having something like:
handlers:
- url: .*/mobiler
script: mobile-frontend.v_one.mobiler.app
Perhaps you're doing a little too much in the appengine_config.py, try reducing it to:
import sys
sys.path.insert(0, 'lib')