skip to Main Content

I’m opening the same python project in pycharm and everything is working well
opening same folder on vs code with same interpreter
I’m getting error ‘Unable to import’ on local modules in the project
what do i miss

Example tree

MyFile.py
│
├── csv (standard library)
├── io (standard library)
├── re (standard library)
│
|--  __init__.py
|
├── myproject.myfolder.mymodule
│   ├── methodA
│   ├── methodB
|   └-- __init__.py

MyFile.py

import csv
import io
import re

from myproject.myfolder.mymodule import methodA, methodB

in pycharm it working well, in Vs-code getting unable to import exception.
Note – i tried to create workspace and venv, not working either

Thanks in advance

2

Answers


  1. Are methodA and methodB python files? In that case you would have to do

    from myproject.myfolder.mymodule import methodA 
    from myproject.myfolder.mymodule import methodB
    

    Also make sure you run your program from the same directory as your myfile.py

    Login or Signup to reply.
  2. You need to add init.py in the folders where you have your modules

    └── src                <- Source code for use in this project.
        ├── __init__.py        <- Makes src a Python module.
        │
        ├── main.py            <- main python script
        │
        ├── modeling
        │   ├── __init__.py    <- Makes modeling a Python module.   
        │   ├── predict.py     <- Code to run model inference with trained models.
        │   └── train.py       <- Code to train models.
        │
        ├── utility           <- Scripts that serves as modules to import to make workflow more efficient.
        │   ├── __init__.py    <- Makes utility a Python module.
        │   ├── plots_cfg.py   <- Code to store plotting configuration.
        │   └── config.py      <- Code to store useful variables and configuration.
    

    main.py

    from utility import config
    from modeling import predict
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search