1

I'm trying to run this line of code from the beginning of a python script in pycharm:

!pip install pyknow

but it throws an invalid syntax error. In jupyter notebooks this magic command would work, but it seems is not the same here. What's the correct of doing this please?

EDIT

the script where i'm trying to run this command from is a .py file

Thank you very much in advance

Miguel 2488
  • 1,410
  • 1
  • 20
  • 41
  • 1
    Have you tried to install de environment and then create a run configuration with that virtual environment? – NicoT Sep 16 '19 at 08:09
  • well, i have a requirements.txt file that would install all of the necessary packages for the project but when i deploy my app as a web service in the cloud, i'm getting a `moduleNotFound` error coming from `pyknow`. This is driving me nuts because the package is in the requirements file and installed in my local machine, but for some reason, my cloud environment doesn't seem to get it :/ – Miguel 2488 Sep 16 '19 at 08:17

3 Answers3

4

You could try something like this:

import os
os.system('pip install pyknow')

This would run the pip install pyknow command in terminal, which is where you would usually put it.

If you have a requirements file, then I think you can run:

import os
os.system('pip install -r requirements.txt')
Ivan Novikov
  • 558
  • 6
  • 13
  • Thank you very much @Ivan Novikov, this seems pretty much what i wanted. I'll try it right now and i will see if it works, then i'll accept your answer – Miguel 2488 Sep 16 '19 at 08:31
  • Allright, it seems to work. but then i get this error after it starts `OSError: [Errno 48] Address already in use` any ideas? – Miguel 2488 Sep 16 '19 at 08:32
  • Not sure, but from a quick Google it doesn't seem to be related to using `pip`. Have a look at this post: https://stackoverflow.com/questions/51309895/oserror-errno-48-address-already-in-use I would also suggest looking into other ways of properly deploying a python script in the cloud, for example creating a `setup.py` https://setuptools.readthedocs.io/en/latest/setuptools.html – Ivan Novikov Sep 16 '19 at 08:47
2

You should edit the command to

pip install pypi

Because the (pypi) is the name of package (pyknow)

madprogramer
  • 599
  • 3
  • 12
  • 36
1

pip is not a python script... it's a bash script. you can't just run it from a python script...

if you are trying to have a script that will take care of the dependencies I would suggest creating a bash script that would install what it needs then execute the .py script.

Roee N
  • 502
  • 2
  • 4
  • 17
  • Thank you very much for your answer @Roee N. So, you are saying there is no way to run this from a py file in a jupyter like way at all? – Miguel 2488 Sep 16 '19 at 08:17
  • it's not a Jupyter issue at all, it's a Python issue, you are trying to run a script which was written for Bash (pip) in a Python script... those are 2 different script languages – Roee N Sep 16 '19 at 08:23