#๐Ÿ”’ Python import (VS code)

55 messages ยท Page 1 of 1 (latest)

leaden pilot
#

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?

velvet hedgeBOT
#

@leaden pilot

Python help channel opened

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.

analog hill
#

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

leaden pilot
#

from launch.json to .vscode?

analog hill
#

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

leaden pilot
#

Yeah, I don't.

analog hill
#

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

leaden pilot
#

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

leaden pilot
#

Why is it working fine for everyone?

lethal horizon
#

I heard that PyCharm solves this for you, but personally I use vscode so idk how true that is

lethal horizon
# leaden pilot

Yeah well you don't have what I posted, so it doesn't work

leaden pilot
#

aaa

#

second

leaden pilot
#

imports have been written in main

lethal horizon
leaden pilot
#

aaa

#

import

lethal horizon
#

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
leaden pilot
lethal horizon
leaden pilot
#

same

lethal horizon
# leaden pilot

which file did you try to run? (it cutoff in the screenshot)

leaden pilot
#

tryg

lethal horizon
#

E.g.

  • If I run main.py here, no errors
  • If I run apple.py, it'll tell me relative import error
leaden pilot
#

I made it run in main with a function call, but still nothing works.

#

stop

#

worked

lethal horizon
leaden pilot
#

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?

lethal horizon
leaden pilot
lethal horizon
leaden pilot
analog hill
#

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

velvet hedgeBOT
#
Python help channel closed

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.