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.
Antonio SEO
  • 247
  • 5
  • 19

2 Answers2

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

Jojin
  • 114
  • 8
  • 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 from ..dto import PriceInfo – Antonio SEO Dec 14 '18 at 12:38
  • Can you check this again?? – Antonio SEO Dec 14 '18 at 12:39
  • 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. – Jojin Dec 14 '18 at 13:11
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.

Yossarian42
  • 1,584
  • 12
  • 12