Hello, I am tired of looking for a solution to the problem with import in VS code. If anyone can help me I will be grateful.Import is from tryg in main (this is an example), the thing is that import is not possible neither with relative import nor with import from native file, it constantly writes that the module is not found. I found a solution to this problem, which is to write before importing os.path to the file from which the function is imported or something else. But when examining other people's code, I don't find any such methods. Can anyone help with this?
#๐ Python import (VS code)
55 messages ยท Page 1 of 1 (latest)
@leaden pilot
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
i think problem with vscode, that it sets working dir to dir of your file
os.getcwd()
im pretty sure if you set os.chdir() to your src dir, it will start recognising folders as modules
And how to implement it?
from launch.json to .vscode?
go to settings icon on lower left corner of vscode
emm hmm. that depends if you launch your vscode from repo root
oh and maybe you are not using jupyter haha. my bad
Yeah, I don't.
i think stuff in launch.json is for debugging purposes, you can add "cwd" key there. idk how to change run w/o a debug
Some kind of crappy problem. Although pycharm does not have this problem inside the IDE, but for example in the terminal itself there will be a problem
Why is it working fine for everyone?
relative import
That will work if you instead had this
main.py
my_lib/
| - gggq/
| | - tryg.py
| - hit_pp/
| | - other.py
# other.py
import ..gggq.tryg
# main.py
import my_lib
...
I heard that PyCharm solves this for you, but personally I use vscode so idk how true that is
You could also try set the PYTHONPATH env variable
Yeah well you don't have what I posted, so it doesn't work
imports have been written in main
That's still not what I wrote
main.py should be in the same level as src in your case
some_top_level_folder/
main.py
src/
gggq/
__init__.py
tryg.py
hit_pp/
__init__.py
other_s.py
It still doesn't work
What's the error msg?
which file did you try to run? (it cutoff in the screenshot)
tryg
and that won't work, you run main.py then inside main.py call stuff in tryg
E.g.
- If I run
main.pyhere, no errors - If I run
apple.py, it'll tell me relative import error
Well it still doesn't work fully, but you're closer to it working
It's now a different error saying no pop_a is in tryg
That was my mistake.
But
When I see these launches from a neighboring folder in other people's vids, for some reason everything works, how so?
or should I always run the file from the topmost directory when launching the code?
Honestly I'm not really sure either
I have been struggling with this for 3 days and found only a couple of good videos with my problem, and it is solved only with crutches, although in pycharm everything worked fine.
PyCharm probably does some magic behind the scenes (changing PYTHONPATH/sys.path without you knowing or something, idk cause again I don't use it)
Yes, it does this trick and I figured out how, and I'm trying to figure out how to do the same in VS code.
when i launch reglar .py files in vscode, it sets working dir to my root
i can set working dir to file directory and add python path via launch,json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${fileDirname}",
"env": {
"PYTHONPATH": "${workspaceRoot}", // "${workspaceRoot}"${workspaceFolder}
}
}
]
}
it seems that "Python: Current File" config runs for both debug and non-debug mode
but again. i can just assume that you have launched your vscode by setting work root to src folder
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.