TJMcButters
TJMcButters

Reputation: 43

Importing a python file from another python file in VSCode

I've seen many attempts at a fix for this question, but not have gotten it right for me. I love VSCode - it's good looking, lightweight, and fast. But I cannot understand how they made import statements so absolutely confusing. For context, I'm coming from Pycharm where everything just worked (after 10 minutes of waiting for the program to startup).

So, I have a folder structure that looks something like this:

enter image description here

How the interactions work currently:

  1. a.py and b.py can import with each other just fine with: import (a|b).py
  2. a.py and b.py can import with c.py and d.py just fine with: from folder(1|2) import (c|d).py
  3. c.py and d.py cannot import with a.py or d.py with: from . import (a|b).py (which has been a recommended solution on other questions of a similar nature as this one)
  4. c.py and d.py cannot import with each other with: from .folder(1|2) import (c|d).py (I'm pretty sure the syntax is wrong here anyway)

My current settings.json is:

"python.pythonPath": "c:\\dev\\workspace-vscode\\Python\\Personal\\importTesting\\venv\\Scripts\\python.exe"

and my launch.json is:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "integratedTerminal",
        "cwd": "${workspaceFolder}"
    }
]}

I've seen solutions involving a .env file, but they never explain where to put the file. Also, what is the workspace folder? Where does that resolve to? Is it considered the root folder?

Something else to note is that the actual import statement that I'm using is: from Code.WordAnalysis.wordsAndLetters import word_count, which I understand will mean nothing to you unless you look at my code (which I've linked to below). But the goofy thing about that is that it works in Pycharm without breaking a sweat. This behavior makes me think it's some referencing issue - could be wrong.

The above import statement spits out this error:

Traceback (most recent call last): File "c:\dev\workspace-vscode\Python\WorkRelated\ITTechTalk\Code\PasswordStrengthCalculators\passwordstrengthreal.py", line 1, in <module> from Code.WordAnalysis.wordsAndLetters import word_count ModuleNotFoundError: No module named 'Code'

In case you want to look at my repository, it can be found here: https://github.com/TJMcButters/workspace-vscode/tree/master/Python/WorkRelated/ITTechTalk

Any help with this would be very much appreciated! It's driving me crazy.

Upvotes: 1

Views: 4502

Answers (1)

Roman Ferenets
Roman Ferenets

Reputation: 374

Response to cases number 3 and 4:

When running a script directly, it is impossible to import anything from its parent directory.

My approach is to avoid writing scripts that have to import from the parent directory. In cases where this must happen, the preferred workaround is to modify sys.path.

You can add directories from which you want to import modules in a runtime, by doing something like this:

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

I would recommend you to have a look on this material - https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html.

Upvotes: 3

Related Questions