0

I'm workig in VS code and have cloned an internal repository.This repository comes with some default scripts. I can start a container and open up jupyter notebook from this. If I do that I can easily use functions across the different scripts like if want to get function Z from sql.sql_import import Z. But if I try to do exaclty the same thing but in VS code, it keeps saying "No module named sql.sql_import" and "sql is not a package". Do you guys know what can be done to solve this? I'm pretty new to this, but I hope it makes sense.

The code I use in the script where I want to import the function:

from sql.sql_import import Z

A section of the project structure:

AnonymizedEmbeddings
├─ Makefile
├─ README.md
├─ src
│  ├─ main
│  │  ├─ __init__.py
│  │  ├─ import_data.py
│  └─ sql
│     ├─ __init__.py  
│     └─ sql_import.py

I have tried moving around the scripts so they are in the same folder

  • Do you have a \_\_init\_\_.py file in your folder ? – rochard4u Jul 05 '23 at 10:08
  • @rochard4u Yes I have tried adding "__init__.py" in all the folders I have – Mads-Emil Hvid Rasmussen Jul 05 '23 at 10:11
  • check the folder in which your workspace is. example, when you open terminal, the last word on the terminal line is your current working directory. paths should be from here – manjy Jul 05 '23 at 10:12
  • @manjy In the terminal it looks like this: xxx@xxx:~/AnonymizedEmbeddings/src/main when I try to run the import_data.py (see edit in post, I have linked picture of structure) – Mads-Emil Hvid Rasmussen Jul 05 '23 at 10:20
  • @Mads-EmilHvidRasmussen I dont see src in the picture you have uploaded. also from which file are you trying to import, and what are you trying to import – manjy Jul 05 '23 at 10:32
  • @manjy My bad I have updated the picture. Im trying to import a function from sql_import.py into the script import_data.py – Mads-Emil Hvid Rasmussen Jul 05 '23 at 10:47
  • Please replace the image with a text in the question! [Read this](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors/285557#285557)! Better format the question: `from X.Y import Z` not "from X.Y import Z"! – Mohammed Samir Jul 05 '23 at 10:50
  • Try `from src.main.import_data import Z`! You need the relative path to the root directory of the project. – Mohammed Samir Jul 05 '23 at 10:53
  • @MohammedSamir By doing that I get: ModuleNotFoundError: No module named 'src' – Mads-Emil Hvid Rasmussen Jul 05 '23 at 11:04
  • You don’t have `__init__.py` in your `src` directory, so it’s not recognized as a module/package. Try adding an empty `__init__.py` there and use `from src.main.import_data import Z`. – Ash Jul 05 '23 at 11:10
  • @Ash I have now tried adding a ```__init__.py``` file to the src directory, but ```from src.main.import_data import Z``` results in this: ```ModuleNotFoundError: No module named 'src'```. The ```sql_import.py``` is located in the sql folder, so shouldn't it be ```from src.sql.sql_import import Z``` as is want to get a function from ```sql_import.py``` to ```import_data.py``` – Mads-Emil Hvid Rasmussen Jul 05 '23 at 11:16
  • check this out https://stackoverflow.com/a/28712742/8464994 currently you are in main directory, you need to import modules from parent directory, i.e. source, so try this solution about importing modules from parent directory – manjy Jul 06 '23 at 11:49
  • May I know what's going on with the problem? If it has been solved, can you change the status of the answer to help more people with similar problems. – JialeDu Jul 31 '23 at 02:09

2 Answers2

0

Add the following code at the top of the import_data.py code:

import sys
sys.path.append("./src")

enter image description here

If you are willing to move the script to the same folder, you can import directly without the above method.

JialeDu
  • 6,021
  • 2
  • 5
  • 24
0

Add following line on top of your code :

import sys
sys.path.insert(0, '..')

Check the following source out: https://stackoverflow.com/a/28712742/8464994

manjy
  • 109
  • 1
  • 2
  • 12