skip to Main Content

i’m working on visual studio code and can’t import files from the project’s folder
that’s the folder of the project

a code

import frame

is underlined on the word "frame" and produces following output:

Import "frame" could not be resolvedPylancereportMissingImports

i tried many things and solutions given on similar problems on SO, i checked global variable ComSpec, it’s value is proper – C:WINDOWSsystem32cmd.exe, i installed , i set "python.analysis.extraPaths" to ["frame", "charts", "app"] and gave value to python.defaultInterpreterPath (its previous value was just ‘python’). what is interesting – i ahd no settings.json value before, i created it myself by opening settings.json in visual studio code (i can’t edit settings.json when opened by using ctr+shift+p), copying contents and pasting it in file settings.json which i created in .vscode folder – it was not created by visual studio code although i’m using virtual environment

2

Answers


  1. Edit:

    Docs on Importing Jupyter Notebooks as Modules

    Solution 1

    I also found this other answer, using import-ipynb, that I’ll paste below:

    Install my helper library from the command prompt:

    pip install import-ipynb
    

    Import it from your notebook:

    import import_ipynb
    

    Now import your .ipynb notebook as if it was a .py file

    import TheOtherNotebook
    

    This python-ipynb module is just one file and it strictly adheres to the official howto on the jupyter site.

    PS It also supports things like from A import foo, from A import * etc

    PPS Works with subdirectories: import A.B

    Solution 2

    On the same question as linked above, there is this additional answer that could be helpful as well, I’ll paste it below:

    Run

    !pip install ipynb
    

    and then import the other notebook as

    from ipynb.fs.full.<notebook_name> import *
    

    or

    from ipynb.fs.full.<notebook_name> import <function_name>
    

    Make sure that all the notebooks are in the same directory.

    Edit 1: You can see the official documentation here – https://ipynb.readthedocs.io/en/stable/

    Also, if you would like to import only class & function definitions from a notebook (and not the top level statements), you can use ipynb.fs.defs instead of ipynb.fs.full. Full uppercase variable assignment will get evaluated as well.


    Try turning the directory into a package by adding a __init__.py file. Here’s an example of how it would work:

    Directory Structure:

    ProjectRoot:.
    │   frame.py
    │   other.py
    │   __init__.py
    

    frame.py:

    MSG = "Hello World"
    

    other.py:

    import frame
    print(frame.MSG)
    

    __init__.py:

    # Empty file
    

    If you were to run other.py, you’d get:

    Hello World
    

    Heres a link to documentation on python’s import system

    Login or Signup to reply.
  2. Simple and straightforward way is to convert your frame.ipynb file to .py file.

    enter image description here

    enter image description here

    Also about the configuration of "python.analysis.extraPaths". The scripts are all in the same folder, and "python.analysis.extraPaths" does not need to be configured at this time.

    enter image description here

    If subfolders exist, the configuration is as follows:

        "python.analysis.extraPaths": [
            "./pyfile"
        ]
    

    It is the path, not the file name

    enter image description here

    is the path, not the file name

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search