I had a file at db/connection.py
. I moved the file and now the path is project/db/connection.py
.
Previously I imported that file using this syntax:
import db.connection
And somewhere in the code I had access to the file:
db.connection.open_connection()
Since now the file is moved, I know only two options to import the file: using import
and from import
keywords. But both of them will change the reference to the file. I’ll show you what I mean.
If I use import
:
import project.db.connection
Then the new reference is project.db.connection
and I need to change the code that accesses the file as well, so that it becomes:
project.db.connection.open_connection()
If I use from import
:
from project.db import connection
The same problem, but now the reference is connection
.
I thought I could do that:
from project import db.connection
But python gives me an error at that line of code:
from project import db.connection
^
SyntaxError: invalid syntax
So if it’s possible, how to import the new file having the reference db.connection
, so I don’t need to change the code that accesses that file?