3

My issue is that i am able to import a python module in the terminal using the shell but when i import the same module in a file, i get an error. This is the message i get:

Traceback (most recent call last):
  File "/home/dalcoy/Desktop/Projects/Scripts/something.py", line 1, 
in <module>
import pygame
ImportError: No module named pygame
[Finished in 0.0s with exit code 1]
[shell_cmd: python -u 
"/home/dalcoy/Desktop/Projects/Scripts/something.py"]
[dir: /home/dalcoy/Desktop/Projects/Scripts]
[path:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

this is the program:

import pygame

Any solution?

Colday96
  • 81
  • 4

2 Answers2

0

You must install the module first in order to use it in your script. Run py -m pip install pygame to install the module. Then it will work fine in your script

ntrupin
  • 83
  • 1
  • 6
0

Check what kind of python version you're using, as the python shell might be 2.7 and when you try to launch it as a script from a file it could be interpreted by different python version, for e.g. python3 which doesn't have this module installed.

To check:

# which python

or

# which python3 

You can also call a file like this to specify which python version you want:

# python3 /path/to/file.py 

or

# python /path/to/file.py

You can also make a file an executable using

# chmod +x /path/to/file.py

And adding hashbang to the first line of the file:

#!/usr/bin/env python3

To then be able to run a python script like this:

# ./my_script.py

Here you can read on managing packets with pip for different versions:

pip: dealing with multiple Python versions?

Bohdan
  • 23
  • 4