0

While I run and debug my project in PyCharm, currently using Python 3.10, I also often prefer to edit my code in VS Code. The file structure of my project looks like:

my_project_folder
├─ subfolder_1
├─ subfolder_2
├─ …
├─ __init__.py
└─ application.py

Import statements in my code look like

from subfolder_1 import func1, func2

and it works perfectly in PyCharm. However this form of statement raises pylint import error Unable to import 'subfolder_1' in VS Code.

At the same time if I change an import statement like

from my_project_folder.subfolder_1 import func1, func2

it raises no errors in VS Code, but I can not run the project in PyCharm due to the error Unresolved reference 'my_project_folder'.

As for now I am not going to work on any new python project, so I did make no special configuration changes (like adding virtual environment, etc.) either in PyCharm or in VS Code.

Is it possible to change configurations in PyCharm and/or VS Code to make any form of import statement acceptable for both of them?


UPDATE. As a temporary solution I have just suppressed the disturbing error message in VS Code. It is explained here why to do this in the right way you should go to Workspace Settings, find 'Pylint Args' and add three items:
--disable=all

--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode

--disable=F0401
After this imports that are really wrong are still marked by another color in VS Code color scheme so I can see my mistakes.

1 Answers1

0

Maybe you can use the sys.path.append() method.

import sys
sys.path.append("put_your_path_here")


The original answer was overly simplistic, re-edited as follows:

First of all you need to know that VS Code uses your currently open folder as the workspace. I created the file structure in your question and opened it in VS Code, they are in the same folder, and importing them like this doesn't give an error.

enter image description here

So maybe you are opening another folder or there should be other code causing this error.

about your comment:

You can import like this at the beginning of your code:

import sys
# Just an example, please modify it according to your file directory
sys.path.append("C:\\Users\\Admin\\Desktop\\New_Test\\my_project_folder\\subfolder_1")

from subfolder_1 import func1, func2
JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • Thank you for the update. I should check why this does not work for me. I guess it's because some pylint arguments are also defined in PyCharm. – Kostiantyn Ivashchenko Jul 19 '22 at 17:12
  • I'm sorry this didn't work for you. Can you provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) for me to reproduce your problem? – JialeDu Jul 20 '22 at 01:53
  • @JialeDo, the problem is that a certain installation of PyCharm is needed to reproduce the issue completely. At least VS Code behavior changes, when I, say, change the root folder for the corresponding PyCharm's project. After I applied a temporary solution to suppress the annoying messages (see the update above), I am not sure I'll ever investigate the issue further. Anyway I greatly appreciate your efforts and your wish to help me. Thank you! – Kostiantyn Ivashchenko Jul 20 '22 at 13:12