SOS. I'm in the process of migrating my SQLite database to MySQL and I'd like to share my MySQL connection using a function in /db/connect.py with my app at /app/app.py. I'm using a temp file to test the connection at /app/test.py which contains the function import. The connection from /db/connect.py runs successfully by itself. However, even after following other guides to the letter, my function in /db/connect.py isn't importing correctly. Even after adding an empty __init__.py file in the /db/ directory.
What I've tried
Here's a small sampling of what I've tried here, here, here, and here (as well as all other questions in stack overflow on the topic, e.g., here, here, here, here, etc).
/db/test.py (temp file with function import)
Imports /db/connect.py > sql_db_connection().
from db.connect import sql_db_connection
import db
def get_all():
conn = sql_db_connection()
cursor = conn.cursor()
cursor.execute("select * from posts")
result = cursor.fetchall()
for x in result:
print(x)
Error (error from running test.py)
Traceback (most recent call last):
File "/Users/<uname>/<my-long-app-name>/app/test.py", line 1, in <module>
from db.connect import sql_db_connection
ModuleNotFoundError: No module named 'db'
/db/connect.py (function for mysql connection)
import sshtunnel
import MySQLdb
import os
from dotenv import load_dotenv
# Load all env vars
load_dotenv()
def sql_db_connection():
sshtunnel.SSH_TIMEOUT = 5.0
sshtunnel.TUNNEL_TIMEOUT = 5.0
with sshtunnel.SSHTunnelForwarder(
('ssh.pythonanywhere.com'),
ssh_username=os.getenv("REMOTE_DB_SSH_USERNAME"), ssh_password=os.getenv("REMOTE_SSH_PASSWORD"),
remote_bind_address=(
os.getenv("REMOTE_BIND_ADDRESS"), 3306)
) as tunnel:
conn = MySQLdb.connect(
user=os.getenv("REMOTE_DB_SSH_USERNAME"),
passwd=os.getenv("REMOTE_DB_PASSWORD"),
host='0.0.0.0', port=tunnel.local_bind_port,
db=os.getenv("REMOTE_DB_NAME"),
)
print('Connected!')
return conn
sql_db_connection()
Tree
.
├── Dockerfile
├── README.md
├── __pycache__
│ ├── app.cpython-310.pyc
│ ├── app.cpython-39.pyc
│ ├── config.cpython-39.pyc
│ ├── wsgi.cpython-38.pyc
│ └── wsgi.cpython-39.pyc
├── app
│ ├── Dockerfile
│ ├── __init__.py
│ ├── __pycache__
│ ├── app.py
│ ├── config.py
│ ├── public
│ ├── requirements.txt
│ ├── templates
│ ├── test.py
│ └── wsgi.py
├── db
│ ├── Dockerfile
│ ├── README.md
│ ├── __init__.py
│ ├── __pycache__
│ ├── connect.py
│ ├── database.db
│ ├── dump.sql
│ ├── init_db.py
│ ├── mysql_dump.sql
│ ├── schema.sql
│ └── schema_mysql.sql
├── docker-compose.yml
├── features.md
├── images
├── requirements.txt
├── setup.py
├── venv
│ ├── bin
│ ├── include
│ ├── lib
│ ├── man
│ └── pyvenv.cfg
└── wsgi.py
add these two lines as the first two lines in test.py
import sys
sys.path.append('/Users/<uname>/<my-long-app-name>')
Related
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
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 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
I am somewhat still unfamiliar with Django so please excuse the possible elementary question.
am trying to deploy a Django project, but I am having some problems with it.
The project works exactly as it should on Windows, however I am getting this error on my Ubuntu VPS:
Unhandled exception in thread started by <function wrapper at [...]>
Traceback (most recent call last):
[...]
Stack trace from system/django files
File "/home/django/[Project Name]/apps/sale/admin.py", line 8, in <module>
from .forms import SaleRequestFormAdmin
File "/home/django/[Project Name]/apps/sale/forms.py", line 6, in <module>
from apps.listing.models import Listing
ImportError: No module named listing.models
My structure for the project is like this (with irrelevant components removed or shortened):
.
├── apps
│ ├── [...]
│ ├── listing
│ │ ├── admin.py
│ │ ├── api.py
│ │ ├── apps.py
│ │ ├── forms.py
│ │ ├── __init__.py
│ │ ├── migrations
│ │ │ ├── [...]
│ │ ├── models.py
│ │ ├── tests.py
│ │ └── views.py
│ ├── sale
│ │ ├── admin.py
│ │ ├── api.py
│ │ ├── apps.py
│ │ ├── forms.py
│ │ ├── __init__.py
│ │ ├── migrations
│ │ │ ├── [...]
│ │ ├── models.py
│ │ ├── tests.py
│ │ └── views.py
├── __init__.py
├── [Project Name]
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
The apps folder contains all of my Django apps, and is in the same folder as manage.py and my project folder.
You can see that there is a file called models.py in the listing app.
Does anyone have any idea why this does not work on Linux? I couldn't find any relevant information on Google, and I really don't know why it is failing.
Any help would be greatly appreciated.
I think the problem is that apps path is not in your sys path
Try adding the following in your settings.py file:
import os
import sys
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))
Let know if it helps!
I've been googling this all day and have not come across a solution, hope you can help:
Using Django 1.6, I made an app called "builds" as part of a project called "computerbuilder".
The site works fine when I use the test server, however I created a file to populate the database with some items, and it's giving me the error when I run python fillDB.py:
Traceback (most recent call last):
File "fillDB.py", line 1, in <module>
from builds.models import BuildsTable
ImportError: No module named builds.models
This is my file fillDB.py:
from builds.models import BuildsTable
moboDB = open("db.txt", "r")
lines = moboDB.read().split('\",')
print lines
def main():
global lines
global BuildsTable
for item in lines:
mobo = BuildsTable.objects.get(moboListing="%s" % item[0])
price_local = BuildsTable.objects.get(moboListing="%s" % item[1])
if(BuildsTable.objects.filter(
moboListing = mobo, price = price_local).exists() == False):
mydb = BuildsTable(moboListing = mobo, price = price_local)
mydb.save()
main()
This is my models.py file from the "builds" app I made:
from django.db import models
# Create your models here.
class BuildsTable(models.Model):
id = models.AutoField(primary_key=True)
moboListing = models.CharField(max_length=200)
price = models.IntegerField()
My directory looks like this:
├── builds
│ ├── admin.py
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── computerbuilder
│ ├── dev
│ │ ├── db.txt
│ │ ├── fillDB.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ ├── wsgi.py
├── manage.py
└── requirements.txt
Since I'm using an external file not part of Django, I think that's why its having trouble recognizing it. Also using postgres if that helps.
You need to add the init.py in dev folder. Because it treated as a python package. Next you need to add your django project path in fillDB.py like this,
Root
├──├── builds
│ ├── admin.py
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── computerbuilder
│ ├── dev
│ │ ├── db.txt
│ │ ├── fillDB.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ ├── wsgi.py
├── manage.py
└── requirements.txt
Follow the above folder structure,
And also you need to set the django environment variable to this file.
fillDB.py
import sys
import os
if __name__ == "__main__":
sys.path.append('/path/Root')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "computerbuilder.settings")
from builds.models import BuildsTable
mobo = BuildsTable.objects.all()
print mobo
Hope this help you
Check you sys.path to see whether there have you path or not. Every time you run your python project, python will append you current path to the sys.path. And once you quit the python enviroment, python will remove the path you appended in.
Your problem is you run just run fillDB.py, python just append '../computerbuilder/dev' into sys.path, so python can not find builds module.
The solution is move your fillDB.py file to the same level as builds folder
├── builds
├── fillDB.py
│ ├── admin.py
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── computerbuilder
│ ├── dev
│ │ ├── db.txt
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ ├── wsgi.py
├── manage.py
└── requirements.txt
Hope it can help you :D