2

enter image description here

I created my customized package called 'dto' in my project folder. But It does not recognize this package and module.

How can I make my visual studio code to find it?

In Pycharm, if I create new package, it automatically detects that.

  • I executed my simulator.py script in my simulation package.

3 Answers 3

2

I have encountered the same problem. It seems visual studio code cannot automatically detect new python package. It has something to do with $PYTHONPATH configuration. I found an official reference from visual studio code documentation. Please have a look at this doc.

  1. adding a dev.env file inside your project
PYTHONPATH=${workspaceFolder}:${PYTHONPATH}
  1. adding the following in your workspace settings.json config file
"python.envFile": "${workspaceFolder}/dev.env"

This works for me. The debugger can find modules in the new package. Hopefully, this will help you.

Sign up to request clarification or add additional context in comments.

Comments

2

From what I can see from the directory tree, you need to use a relative import(python >= 2.5):

from ..dto import price

Here the .. is used to specify that the import should be made from two folders up the current location of the script that is being invoked.

In your case, relative imports cannot be used as the files are in different packages. Please find the relevant post here beyond top level package error in relative import

3 Comments

Thank you for helping me. This error occurred. Exception has occurred: ValueError attempted relative import beyond top-level package File "C:\Users\lich_\Documents\aits\simulation\simulator.py", line 4, in <module> from ..dto import PriceInfo
Can you check this again??
Oops, I might have jumped the gun there. Relative imports cannot be used beyond the current package. I have added the relevant answers to your question in my post.
0

@Yossarian42's answer will work if you have a folder with your package name in the root of your project. However if your project follows the structure mentioned in Py-Pkgs and uses a src dir like:

mypkg
├── CHANGELOG.md               ┐
...
├── pyproject.toml             ┐ 
├── src                        │
│   └── mypkg                  │ Package source code, metadata,
│       ├── __init__.py        │ and build instructions 
│       └── mypkg.py           ┘
└── tests                      ┐
    └── ...                    ┘ Package tests
└── examples                   ┐
    ├── mypkg_examples.py      │
    └── ...                    ┘ Package examples

In this case, you use the following for dev.env in your workspaceFolder (root):

PYTHONPATH=./src:${PYTHONPATH}

Create or edit {workspaceFolder}/.vscode/settings.json with:

"python.envFile": "${workspaceFolder}/.env"

Full settings.json example:

{
    "python.envFile": "${workspaceFolder}/dev.env"
}

For debugging the python.envFile setting, you can print out the Python path with the following code:

import sys; print(f"sys.path: {sys.path}")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.