-2

I am trying pip commands. I am installing OpenAI Gym. I wrote the following command in a notebook cell and run it: pip install gym It worked and gave me a message of successful installation.

But when I tryed typing the same command in a .py script file and run it, it gives me an error:

    pip install gym
        ^
SyntaxError: invalid syntax

I know that I already have the gym package installed on my machine since the successful execution in notebook. However, I think it should give me different message that it is already installed or updated like when I rerun the command again in notebook cell:

Requirement already satisfied: gym in c:\users\osama\anaconda3\lib\site-packages (0.20.0)
Requirement already satisfied: numpy>=1.18.0 in c:\users\osama\anaconda3\lib\site-packages (from gym) (1.20.3)
Requirement already satisfied: cloudpickle>=1.2.0 in c:\users\osama\anaconda3\lib\site-packages (from gym) (1.6.0)
Note: you may need to restart the kernel to use updated packages.

I just need to understand !

Osama El-Ghonimy
  • 335
  • 2
  • 12

2 Answers2

1

pip is a command line utility that works only on a terminal or command prompt window. Your .py script is a file for python code, and the pip command is not python code, therefore python does not recognize it and it gives you an error.

Here is an official guide on how to get started using pip

ErnestoC
  • 2,660
  • 1
  • 6
  • 19
  • So, why can it be run from notebook? Does this means that notebook can run terminal commands? – Osama El-Ghonimy Sep 21 '21 at 23:52
  • Yes @Osama, a notebook can run terminal commands. Jupyter's Python-based kernel evolved from IPython notebook, which evolved out of the IPython project. All of those can run terminal commands if you use the right syntax. For `pip` you now want you use `%pip install` magic commands, see [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more about that. But a lot of terminal commands have equivalents that automatically get used like `ls`, without you needing to put an exclamation point in front of it. And `%cd` is special. – Wayne Apr 12 '23 at 19:53
1

pip install gym is not python script.You should run it in system terminal like Bash, PowerShell,Cmd and so on to install required package.

sheep
  • 141
  • 4
  • So, why can it be run from notebook? Does this means that notebook can run terminal commands? – Osama El-Ghonimy Sep 21 '21 at 23:52
  • If you add `!` at the beginning of cell in jupyter, you will be able to run shell command after `!`.For example: `!pip install requests` – sheep Sep 22 '21 at 13:51
  • Don't use sheep's specific example. That was outdated when sheep wrote it. At that time and now, always use the magic command `%pip install` in modern, current Jupyter (and related `%conda install`). See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more about the modern magic install commands that insure installation occurs in the environment where the kernel is running that is underlying the active notebook. – Wayne Apr 12 '23 at 19:56