-1

I'm currently learning Python, and I tried importing my own modules but first I got AttributeErrors and then I got ImportErrors (after trying another importing method) and even after searching for 3 hours for a solution I couldn't find one. Restarting VS Code and the Terminal several times and restarting my pc didn't help either. I was about to post my question here and to make it easier to see I moved my 3 files to a new folder. Suddenly it worked without issues! Even after moving the files to the folder I got the errors from it still continued working. I didn't change anything besides moving the files.

Could someone explain why that happend and what caused the errors?

It was structured like this (Code simplified and shortend):

First (didn't work):

C:\Users[User-ID]\OneDrive\Dokumente\Code\Python\hman\hangman_art.py

C:\Users[User-ID]\OneDrive\Dokumente\Code\Python\hman\hangman_words.py

C:\Users[User-ID]\OneDrive\Dokumente\Code\Python\hman\hangman05.py

Second (worked):

C:\Users[User-ID]\Desktop\hangman_art.py

C:\Users[User-ID]\Desktop\hangman_words.py

C:\Users[User-ID]\Desktop\hangman05.py

Then first again (worked too)

hangman_words.py
word_list = [
'abruptly', 
'absurd', 
'axiom'
]

hangman_art.py
logo = ''' 
 _                                             
| |                                            
| |__   __ _ _ __   __ _ _ __ ___   __ _ _ __  
| '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ 
| | | | (_| | | | | (_| | | | | | | (_| | | | |
|_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_|
                    __/ |                      
                   |___/    '''




hangman.py
import hangman_art 
import hangman_words 


print(hangman_art.logo)
print(hangman_words.word_list)




# Resutlted first in AttributeErrors:
AttributeError: module 'hangman_art' has no attribute 'logo' in Python

# Later in IndexErrors after I tried (from hangman_art import logo):
ImportError: cannot import name 'logo' from 'hangman_art'

sp14shb3
  • 13
  • 4
  • So what is the file structure before and the current file structure of these three files? You already know this is the problem why not display this information? – JialeDu Dec 14 '22 at 02:05
  • Does this answer your question? [Why don't other programs see the changes I made to a file in VS Code until I save those changes?](https://stackoverflow.com/questions/76984829/why-dont-other-programs-see-the-changes-i-made-to-a-file-in-vs-code-until-i-sav) – starball Aug 27 '23 at 06:33

2 Answers2

1

The reason I got the Errors first but later not was because as I created the files to import (hangman_words.py and handman_art.py), I didnt save them. When you dont run the code in the file you created, it doesnt get autosaved. I had not closen it either (VS Code asks you if you want to save the changes in the file when you trying to close the tab). After I changed the location, the code in the files got saved, what had solved my importing problem. Thus it worked when I relocated them to the previous folder. Thats it!

sp14shb3
  • 13
  • 4
0

If your previous file does not exist in the same folder or is in a different subfolder, it is normal to have problems importing. In that case you can use the sys.path.append() method to specify the path to import the modules you need.

Also more info about the import statement. Of course if you are willing to Google, you can get more useful information.

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • Thank you for an answer! The odd thing is, the previous files were in the same subfolder. I had already checked the link about the import statement you linked and more websites about it. I guess it was a problem with VS Code itself... – sp14shb3 Dec 14 '22 at 15:57
  • You still haven't shown the file structure. Vscoed uses the currently opened folder as the workspace. If the previous three files are in the subfolders of the folder opened by vscode, there may be problems. You don't show accurate information, it's hard to explain the problem. – JialeDu Dec 15 '22 at 01:41
  • My apologies, JialeDu. I edited the file structure into the question above. I hope this is what you have meant. – sp14shb3 Dec 15 '22 at 23:31
  • sorry bro. Can you explain which level of folder you open in vscode? I have already said that vscode uses the currently open folder as the workspace, which has an impact on importing modules. Maybe a direct screenshot would be clear too. – JialeDu Dec 16 '22 at 01:41
  • The previous explanation was wrong. If all three scripts exist in one folder, no matter which level of folder you open as the workspace, there will be no problem with the import. From the sample code only, if there are other codes in your script, the result may be different. – JialeDu Dec 16 '22 at 01:46
  • I open the "hman" folder when I open the code. The three scripts where all in there, so at the same level as the workspace. There were indeed more scripts in the "hman" folder. I guess VS Code was a bit buggy that day and didnt work as intended. And the change in location may have refreshed the paths of the files again, so VS Code could access them correctly. It workes as it should rn. I thank you a lot for taking the time to help me, as no one else did. Have a great start into the new year bro! – sp14shb3 Dec 17 '22 at 00:14
  • YOOOOOO, JialeDu!!!! I solved the problem! The reason I got Errors first but later not was because as I created the files to import (hangman_words.py and handman_art.py), I didnt save them. When you dont run the code in the file you created, it doesnt get autosaved. I had not closen it either (VS Code asks you if you want to save the changes in the file when you trying to close the tab). After I changed the location, the code in the files got saved, what had solved my importing problem. Thus it worked when I relocated them to the previous folder. Thats it! – sp14shb3 Dec 17 '22 at 04:00