How can i run "pip install" from a pycharm python script? - python

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

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')

You should edit the command to
pip install pypi
Because the (pypi) is the name of package (pyknow)

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.

Related

Why does my code run in the VS terminal but not from the py file?

Beginner here. I've just learned the basics of python using VS. I don't know why I get a syntax error in the VSCode text file but not on the terminal for the command.
Any assistance helping me understand would be great, thank you.
Tried to install boto3 with pip.
You cannot run shell commands from a python script.
This is the right way to do it. You can also use the subprocess module to do it.
import os
# In Linux
os.system("python3 -m pip install boto3")
# In Windows
os.system("py -m pip install boto3")
Although, it's not recommended installing packages inside your code.
You can use a requirements.txt file. Then you just need to run this command once in your terminal:
pip install -r requirements.txt
py -m pip install boto3
Obviously, this does not conform to python syntax.
Usually we call it a command line.
We run it in the shell instead of python file.
Python files will be compiled and then run. This command line statement will not be compiled (As the wavy line in the file reminds, this is an error code). You can further learn Python syntax to learn more about this problem.

Have problem to run Python command installed by pip in macOS Terminal

I'm not a Python user, so know little about Python. But I have to install stcgal using pip3 following its official guide.
pip3 install stcgal
However, if I run the installed command in terminal, it says zsh: command not found.
After a long searching, I find it was installed at /Users/myUserName/Library/Python/3.8/bin/. Though I can run the command with that path, it's very inconvenient. I wonder is there any way to run the code in any directory instead having to locate it explicitly?
Try something like this:
python -m stcgal -h
You can create or edit your /Users/yimingliu/.zshrc file (if your macOS is older than macOS Catalina 10.15, then the file is /Users/yimingliu/.bash_profile), and add a line to add the python bin folder to your path:
export PATH=/Users/yimingliu/Library/Python/3.8/bin:$PATH

How do I use pip install?

When I look up tutorials on Python almost every one starts with "Start of with pip install bs4" but they never tell me about how. Because when I write "pip install bs4" in my cmd the answer I get is only
'pip' is not recognized as an internal or external command,
operable program or batch file.
I've tried running "pip install bs4" in PyCharm as well but nothing works.
I am a totally a beginner, which you probably already noticed, and just want to get beautifulsoup.
I'm going to assume you're on Windows since your error message contains "batch file". Try:
py -m pip install bs4
The -m flag will import the pip module, and allow you to run the install command. If running py doesn't work, try the absolute path to your python.exe.
This is the official site that shows you how to download and install python pip
pretty much just download the file, open up the command prompt, cd to file directory and run it with:
python get-pip.py

How to install a new python module on VSCode?

I'm trying to install new python modules on my computer and I know how to install through the terminal, but I wish to know if there is a way to install a new module directly through VSCode (like it is possible on PyCharm)?
I already installed through the terminal, it isn't a problem, but I want to install without be obligate to open the terminal when I'm working on VSCode.
You should open the terminal inside the VSCode and install the modules you want.
something like👇
if that's not you meant, please let me know.
First of all I would advise you to select the current Python version you have. It has been explained here:
VSCode: There is no Pip installer available in the selected environment
Next, you should check is the pip installed in your Python main directory or not, by checking how to do on this website:
https://pip.pypa.io/en/stable/installing/
or this thread
How to use pip with Visual Studio Code
by typing
py -m pip
in your terminal, like
C:\Users\m\Desktop\Python> py -m pip
You should have the list of commands and general options which can be used. One of them is install
On the Python library platform, you always have the command to be copied in order to the installation of package you want.
In your terminal, the initial command should look as:
PS C:\Users\m\Desktop\Python> py -m
to which you should append the command prepared on the Python library platform (by copying it and pasting).
C:\Users\m\Desktop\Python> py -m pip install openpyxl
That's it. The package should be installed in your Python folder, what you will see in the terminal.
If everything is alright, you just need to type
import openpyxl #or other package name, which you downloaded
and use it!
Unfortunately! for now, only possible way is terminal.

pyInstaller: ImportError: No module named 'praw'

I wanted to pack my script using pyInstaller. I run pyinstaller file.py -F, file is created successfully, but when running I get ImportError: No module named 'praw'. So I created new file containing only import praw and run pyinstaller file.py -F --hidden-import=praw but still get the same error when running.
I was unable to find anything similar, most issues were solved by using --hidden-import.
Any ideas on how it can be solved?
EDIT:
praw is installed inside virtual environment and running the script directly works as expected.
Seem pyinstaller run outside the virtualenv.
Try switch to your virtualenv and run:
python -m PyInstaller -F file.py
I'll recommend to look at pyenv or virtualenv. Activate these env's and install the praw module here. This should work.
This command might help you out. It installs the Praw module for you. Make sure you have pip installed!
pip install praw
I found a way to solve the issue:
When using Python2.7, or starting the shell like python2, we need to do
python2 -m pip install --user praw
to make sure they are linked during installation.
Same idea for python3 shell.

Categories

Resources