Skip to content
Advertisement

os.system() keeps giving me import errors

I am using os.system(“python game2.py”) to run different parts of my code.

Every time I try this it gives me an import error for example “no module named pygame” even though when I game2 itself, it works fine.

What can I do?

Advertisement

Answer

Like said in other answers there is no need to run os.system() if you want to import the code. You can instead use the import function in python.

However I think the reason for the error with the command may be to do with what happens to the directories you are in when you run that command.

When you run the command the directory you are in is the same directory as the code is being run from. Then when you run the game2.py code and it tries to import pygame the reason it can’t find it is because it is only looking the cwd (current working directory) as it normally would however the cwd could be different in the os.system() command.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement