A
A
Aryeh Leonid R.2022-01-26 15:01:56
Python
Aryeh Leonid R., 2022-01-26 15:01:56

How to set up absolute/relative import in Python?

So Visual Studio Code and Python 3.8
The project tree relative to ${workspaceFolder} aka C:\workspace\crypto looks like this:

C:.
└───eyeQ6SerializeDataAPI
    ├───Admin
    ├───db
    └───utils

The relevant fragment of launch.json is:
{
      "name": "Python: uploadBlobs",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/eyeQ6SerializeDataAPI/Admin/uploadSerilizationFile.py",
      "args": [],
      "console": "integratedTerminal"
}

We start F5. Import is relative.
61f1350778698340758522.png
We start F5. Import absolute version 1.
61f13579ae5ec916338906.png
Run F5. Import absolute option 2.
61f135ee2a893820972123.png

It is clear that some of the search paths is not defined.
I suspect that it can/should be defined in launch.json . Update: Thanks everyone! I finally understood what exactly I did not understand, I found the relevant documentation for Python 3.x and Visual Studio Code, tomorrow I will finish reading it and set everything up correctly. Thank you all again. Update 2: This Stack Overflow Q&A really helped... . I was just asking wrong. It's all about configuring the Visual Studio Code runtime environment. Thanks a lot, everyone!



Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dr. Bacon, 2022-01-26
@aryeh

you need the path from eyeQ6SerializeDataAPI to be in Python paths, then it will work, or the path to eyeQ6SerializeDataAPI (without itself) should be in Python paths, then it will workfrom db.что-то_там import чего-то_там

from eyeQ6SerializeDataAPI.db.что-то_там import чего-то_там

A
Andy_U, 2022-01-26
@Andy_U

You may not have __init__.py in your db directory.

A
Aryeh Leonid R., 2022-01-26
@aryeh

I did
from __init__ import *
and it worked. but it doesn't load automatically.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question