-1

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:\WINDOWS\system32\cmd.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

kkkkrkrkkk
  • 21
  • 7

2 Answers2

0

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

Jordan
  • 540
  • 4
  • 10
0

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

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • well, i will need to import 3 files, which i wanted to write in notebook and making copy of each to store it as python file isn't optimal but hey, it works. Thanks – kkkkrkrkkk Sep 19 '22 at 11:03
  • Glad my answer was helpful to you. If you don't mind, you can change the status of the answer, which can help those with similar problems to solve the problem faster. – JialeDu Sep 23 '22 at 06:29
  • it actually didn't work, i came across another problem but after it i just lost hope and decided to not part the project for 3 files xD – kkkkrkrkkk Sep 23 '22 at 10:55