0

I'm using Python in VS code. I am experiencing the error "no module named 'ch6'" when trying to import the functions from one file to another, where the files are located in two different folders.

I have two folders:

  1. "ch6" – which contain the file "RandomCharacter.py". This file has the functions I want to import.
  2. "ch7" – which contain the file "CountLettersInList.py" trying to import the functions from "RandomCharacter.py".

If I try to import the functions with the code:

import ch6.RandomCharacter

I get the error: "No module named 'ch6'"

If I use the code:

import RandomCharacter

I get the error: "No module named 'RandomCharacter'"

However, if I copy CountLettersInList.py from folder ch7 to folder ch6, the import works just fine with the code:

import RandomCharacter

So why can't it import the functions successfully when the files are in two different folders?

KishuInu
  • 61
  • 7
  • Copying `CountLettersInList.py` to folder ch6 allows you to import `RandomCharacter`? That makes no sense. – John Gordon Oct 02 '21 at 04:16
  • Also, is there really a `.py` at the end of `import RandomCharacter.py`? – John Gordon Oct 02 '21 at 04:16
  • It is the case. It works when copying CountLettersInList.py to ch6. Maybe because the files are then in the same folder? – KishuInu Oct 02 '21 at 04:19
  • does the folders have `__init__.py` file present? – Sukhdeep Oct 02 '21 at 04:20
  • It wasn't a .py at the end import RandomCharacter.py. That was typo which now have been edited. – KishuInu Oct 02 '21 at 04:20
  • Where is the file in which you are trying to import? – Nevus Oct 02 '21 at 04:22
  • @Sukhdeep no, `__init__.py` is not present. – KishuInu Oct 02 '21 at 04:22
  • @Nevus The file is in the folder "ch6". – KishuInu Oct 02 '21 at 04:25
  • Try adding `__init__.py` file and check the code again. It marks directories on disk as Python package directories [source](https://stackoverflow.com/a/4116384/7919787). – Sukhdeep Oct 02 '21 at 04:27
  • When importing .py files in the same folder (importing RandomCharacter.py into test.py where RandomCharacter.py and test.py are in ch6 folder) use `import RandomCharacter`. Importing from sibling folder(importing from ch7 into ch6 where ch7 and ch6 both reside in common folder bar) is a bit complex. See [this question](https://stackoverflow.com/questions/16981921/relative-imports-in-python-3) – Nevus Oct 02 '21 at 04:36
  • @Sukhdeep Adding `__init__.py`. Didn't make much of a difference unfortunately. – KishuInu Oct 02 '21 at 04:51
  • @Nevus Thank you. I got only get it working when I copy the `RandomCharacter.py` file from ch6 to ch7 (so that both files are in the same folder). I didn't know that it would be such a hassle to import functions from another directory, even when both folders resides from the same common folder bar. – KishuInu Oct 02 '21 at 04:57
  • @KishuInu. May i know if you ever reached my answer? Has your question been fixed? – Molly Wang-MSFT Oct 07 '21 at 07:27
  • @MollyWang-MSFT Sorry for late reply. I finally got the code working with the code: `import sys sys.path.append('.') sys.path.append('../ch6')` The code runs fine, but for whatever reason, I get the error message: "Import "RandomCharacter" could not be resolved Pylance(reportMissingImports) [5, 8]" – KishuInu Oct 30 '21 at 13:21

3 Answers3

0

The python import system can be annoying. Correct me if I'm wrong in any assumption I make! So what I understand is you're trying to import a RandomCharacter.py into a file located ch7/CountLettersInList.py?

The reason you can't import RandomCharacter.py into CountLettersInList.py is because what CountLettersInList.py can see is only in ch7. It can't even see the folder ch6.

Solution: You have to go up one directory to see ch6. In order to do that tack these lines of code into CountLettersInList.py:

import sys

sys.path.append("..")

This goes up one directory to search for modules!

Kevin Jiang
  • 271
  • 3
  • 10
  • 1
    Thank you for your answer! Your assumption is correct. However, I did not get it working with the code `import sys sys.path.append("..")` I just got the same error. – KishuInu Oct 02 '21 at 15:18
  • is the sys.path.append("..") above the import import ch6 so it looks like ```sys.path.append("..") import ch6 ``` – Kevin Jiang Oct 02 '21 at 17:49
  • Yes, it's above `import ch6.RandomCharacter`. I also tried with just `import RandomCharacter`. Still the same error. – KishuInu Oct 02 '21 at 18:28
  • That's really weird, can you try putting `CountLettersInList.py` in the same directory as `ch6` and `ch7` and temporarily comment out the sys.path.append("..") – Kevin Jiang Oct 02 '21 at 20:35
0

Add the following code in CountLettersInList.py, then you can import successfully.

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

enter image description here

Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22
0

I finally got the code working when I turned on a setting, and then ran the code below:

import sys 
sys.path.append('.') 
sys.path.append('../ch6') 

The setting that I turned on: Setting

The code runs fine, but for whatever reason, I get the error message:

Import "RandomCharacter" could not be resolved Pylance(reportMissingImports) [5, 8] 

Screenshot of error message: Screenshot of error message

KishuInu
  • 61
  • 7
  • Please don't use `Post Answer` to ask follow-up questions. If you can't find the answer via [`Linked`](https://stackoverflow.com/q/69413897/13138364#h-linked) / [`Related`](https://stackoverflow.com/q/69413897/13138364#h-related) / [`Search`](https://stackoverflow.com/search), then use the [`Ask Question`](https://stackoverflow.com/questions/ask) button and include these details (along with a link back to this question if the context would be useful). – tdy Oct 30 '21 at 17:19
  • 1
    @tdy Sorry about that. I get why it would be better for the community to have one question per thread. – KishuInu Oct 30 '21 at 18:22