I know this question is a duplicate but I seriously need some help on it. I have searched everywhere for a solution and none of the ones I found helped solved the issue. I would REALLY appreciate some input.
I am working on a project with the following tree structure:
Project
- Source
- Web
- test
- features
testing.features
- steps
testing.py
- app
- __init__.py
- blueprints
communication_bp.py
I am working on a cucumber test code using the behave module and the folder I am working on is the test folder, which include features and steps.
What I am trying to do is to import some methods found in communication_bp.py from the app folder into the cucumber test file "testing.py" in python.
So Line 3 is what I am trying to achieve.
testing.py:
from behave_restful import *
import requests
from blueprints.communication_bp import client_comm
but I keep getting this error.
File "../steps/testing.py", line 3, in <module>
from blueprints.communication_bp import client_comm
ModuleNotFoundError: No module named 'blueprints'
Based on what I have read, the app folder is supposed to have __init__.py, which it does.
I will include what both this file and communication_bp.py file here as well.
init.py
from flask import Flask
from flask_cors import CORS
from flask_jwt_extended import JWTManager
from flask_socketio import SocketIO
# Create the Flask application
rest = Flask(__name__)
...
...
from app.blueprints.communication_bp import communication_bp
...
...
rest.register_blueprint(communication_bp, url_prefix = Config.API_PREFIX)
...
...
communication_bp.py
import sys
import json
from flask import Blueprint, request
...
...
So what exactly does this program wants? What am I missing here?
I'm struggling to import a folder that has many engines I need to use. I'm importing from main_file.py.
So I think I can use - from engines import qr_code_gen, but I need to import a class which is named _QRCode_ so I tried using - from .engines.qr_code_gen import _QRCode_, but it says "module engines was not found".
Structure:
Server/start.sh
Server/wsgi.py
Server/application/main_file.py
Server/application/engines/qr_code_gen.py
Server/application/engines/__init__.py
...
I used sys.path in main_file.py and I got -
['C:\Users\Dzitc\Desktop\winteka2',
'C:\Users\Dzitc\AppData\Local\Programs\Python\Python37\Scripts\flask.exe',
'c:\users\dzitc\appdata\local\programs\python\python37\python37.zip',
'c:\users\dzitc\appdata\local\programs\python\python37\DLLs',
'c:\users\dzitc\appdata\local\programs\python\python37\lib',
'c:\users\dzitc\appdata\local\programs\python\python37',
'C:\Users\Dzitc\AppData\Roaming\Python\Python37\site-packages',
'c:\users\dzitc\appdata\local\programs\python\python37\lib\site-packages',
'c:\users\dzitc\appdata\local\programs\python\python37\lib\site-packages\win32',
'c:\users\dzitc\appdata\local\programs\python\python37\lib\site-packages\win32\lib',
'c:\users\dzitc\appdata\local\programs\python\python37\lib\site-packages\Pythonwin']
Going from comments you can import engine package.
Try this then:
import engines
engines.qr_code_gen._QRCode_
My project structure is as follows:
my_proj
---calculation
---cardata
---master
---my_proj
--- __init.py__
--- admin.py
--- settings.py
--- url.py
--- update_db_from_ftp.py ///// This is my custom file
In update_db_from_ftp.py I want to download an csv file from ftp and update the database(if necessary) once every day with cron.
But the problem is that I can't import model which I want to update. The model which I want to update is inside master folder in models.py file.
I try to import model as follows:
from master.models import ModelName
But I'm getting an error as follows:
Traceback (most recent call last):
File "update_db_from_ftp.py", line 6, in <module>
from master.models import ModelName
ImportError: No module named master.models
But I use the same model in cardata and calculation folder and I import it the same way as I try in update_db_from_ftp.py file, but there it works without problems.
Any idea how to solve it?
UDPATE
The structure of the master folder is as follows:
For using django code in an arbitrary external script, two things are needed:
Django should be setup (exactly what happens when you run django shell).
The root project should be in python path (not the inner folder with same name).
Here is sample code which will work irrespective of the location of the script (although if the script is not one-off, better to add it as management command):
import django
import os
import sys
sys.path.append("/path/to/my-proj/")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my-proj.settings')
django.setup()
from master.models import ModelName
if __name__ == "__main__":
print(ModelName.objects.all())
I'm having an issue with the import statement in Python 3. I'm following a book (Python 3 Object Oriented) and am having the following structure:
parent_directory/
main.py
ecommerce/
__init__.py
database.py
products.py
payments/
__init__.py
paypal.py
authorizenet.py
In paypal.py, I'm trying to use the Database class from database.py. So I tried this:
from ecommerce.database import Database
I get this error:
ImportError: No module named 'ecommerce'
so I try with both of these import statements:
from .ecommerce.database import Database
from ..ecommerce.database import Database
and I get this error:
SystemError: Parent module '' not loaded, cannot perform relative import
What am I doing wrong or missing?
Thank you for your time!
Add your parent_directoryto Python's search path. For example so:
import sys
sys.path.append('/full/path/to/parent_directory')
Alternatively, you can add parent_directory to the environmental variable PYTHONPATH.
I've been banging my head against this for a while, but can't seem to figure it out.
I've got an app with a set of custom template tags:
from django import template
from crowd.models import Payment, Project, ProjectCategory
register = template.Library()
#register.filter
def is_customer(user, project):
try:
return Payment.objects.filter(user=user, project=project).count() > 0
except:
return False
That throws:
'project_tags' is not a valid tag library: ImportError raised loading crowd.templatetags.project_tags: No module named models
The app tree looks like:
crowd/
-- __init__.py
-- models.py
templatetags/
-- __init__.py
-- project_tags.py
Importing from just models and crowd.models both give me the same error.
Traceback: here
Update
I was working on something unrelated when I noticed this was broken, so I reverted to an earlier, known working version of the project. Still the same problem, so I think Daniels answer about the PYTHONPATH is correct, however, how can I repair this?
>>> import sys
>>> sys.path
['/Users/****/Documents/dev/product/src/Product', ...]
The __init__.py's are all there all the way down, and crowd is in Product, so shouldn't it be on the path?
Update 2
I've done some investigating in the shell:
>>> from crowd.models import *
>>> from crowd.managers import *
>>> from crowd.constants import *
>>> from crowd.templatetags import *
>>> from crowd.templatetags import project_tags
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/Murph/Documents/dev/product/src/Product/crowd/templatetags/project_tags.py", line 4, in <module>
from crowd.forms import SearchForm
File "/Users/Murph/Documents/dev/product/src/Product/crowd/forms.py", line 5, in <module>
from crowd.models import Payment, Project, ProjectUpdate, GalleryPhoto
ImportError: No module named models
>>>
Still don't know why specifically that's failing, especially since the blanket import works.
Update 3
Took me a while to see that the shell command was giving a more useful message than the django one, which led to this:
Turns out it wasn't even related to project_tags directly, it just wasn't a very useful error message. The import in project_tags.py of:
from crowd.forms import SearchForm
was calling:
from crowd.models import Payment, Project, RewardLevel, ProjectUpdate, GalleryPhoto
in forms.py, which should have been:
from models import Payment, Project, RewardLevel, ProjectUpdate, GalleryPhoto
I'll add this as the answer when I can, apparently can't until 8 hours later.
Your crowd app itself is probably not on your Pythonpath. Either add it, or import from the project: from myproject.crowd.models import Foo, Bar.
Took me a while to see that the shell command was giving a more useful message than the django one, which led to this:
Turns out it wasn't even related to project_tags directly, it just wasn't a very useful error message. The import in project_tags.py of:
from crowd.forms import SearchForm
was calling:
from crowdfunder.models import Payment, Project, RewardLevel, ProjectUpdate, GalleryPhoto
in forms.py, which should have been:
from models import Payment, Project, RewardLevel, ProjectUpdate, GalleryPhoto