How to run a pip install command from a subproces.run() [duplicate] - python

This question already has answers here:
Installing python module within code
(12 answers)
Closed 1 year ago.
I see in this article: making and automatic python installer that you can use:
subprocess.run('pip install module_name')
or
subprocess.run('pip install -r requirements.txt')
in this format to install modules either individually or form a file. But when i run this command I get this error:
FileNotFoundError: [Errno 2] No such file or directory:
is there a way to run this like that without having to do this:
subprocess.run(['pip', 'install', 'module_name'])

I suggest you stick to subprocess.run(['pip', 'install', '-r', 'requirements.txt']). To quote the subprocess docs.
Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names).
Avoiding the use of shell=True will save you a lot of trouble if you ever want to run the script on a different system. In general, though, I recommend avoiding reliance on a shell. That is, after all, why you are using Python and not, say, Bash.

To make it work on Linux I used the shell=True parameter. On Windows, it worked perfectly, without it.
subprocess.run("python -m pip install -r requirements.txt", shell=True)
Found the answer here.

Related

conda install doesn't ask for permission before installing modules

I wrote a python script which is being used in githooks/post-merge, and its purpose is to install or update modules from a env.yml file that exists in the same repository. I used the following code:
for module in modules_to_update:
print("\n================================= Updating {} ==================================\n".format(module))
update_command = ['conda', 'install', '{}={}'.format(module.name, module.version)]
subprocess.run(update_command)
As can be seen, modules_to_update contains the modules that will be installed/updated. I didn't use conda update because I didn't want the updates to be automatic.
The problem is: I expected that conda will ask for permission to install/update every module individually, but it didn't work as planned. When I tried it to do so, every module did the same thing- proceeded without waiting for my input:
Proceed ([y]/n)?
Preparing transaction: done
Verifying transaction: done
...
I didn't use the -y flag so it should not work this way, and when I tried using the python script separately it worked just fine.
You're not attaching any stdin to the subprocess. Thus the subprocess likely does a read() call of some sort, gets an empty string (since nothing is attached) and defaults to that [y] yes default in the prompt.
You could do run(..., stdin=sys.stdin), but it might be easier to run an interactive command via os.system(). (Don't use plain string joining to form a command line, though, to avoid shell injection.)
import shlex
update_command = ['conda', 'install', '{}={}'.format(module.name, module.version)]
os.system(shlex.join(update_command))

what is the equivalent of `shell=True` in `paramiko`? [duplicate]

This question already has answers here:
Environment variable differences when using Paramiko
(2 answers)
Closed 1 year ago.
I'm using paramiko's SSHClient to control a server via exec_command method. However, I'm failing to use python over there because it says python not found and I realized the entire conda enviroment is not there. On my local machine, I get around it with shell=True passed to subprocess. Question: how do I do this on paramiko, or, is there another command that I should run to get conda to be loaded up in server's shell.
EDIT: the exec_command offers environment optional keyword but I don't know how to leverage it if it is useful at all.
Thanks to insightful comments by #CharesDuffy with "don't feed me teach me how to fish" style, I solved the problem as follows:
For conda/miniconda to be loaded, I prefaced my commands with source ~/miniconda3/bin/activate;.
Enriching points:
For a permanent solution, the preface can be added to .profile or .bash_profile or .bash_login which is what is loaded up by default when ssh'ing. If you have a fresh installation e.g. a virtual machine, those files don't exist in the first place.
sourcing that conda file is paramount (as opposed to executing it)
Ostensibly, shell=True kwarg of subprocess module is doing something like this behind the scenes.

Python dont find the module when I use it with sudo [duplicate]

This question already has an answer here:
Can't import module when using root user [duplicate]
(1 answer)
Closed 2 years ago.
I am using keyboard library that`s need acces to sudo but when I try to run the program with sudo python dont find the others libraries
import pyautogui
import keyboard
if keyboard.is_preseed('w'):
....
So when I use without sudo keyboard dont work and when I use with sudo python dont find the others libraries
'sudo' sets up a new environment because it aims to be safe and because the new user won't have the same view of the world as you have.
You can use the keep-environment parameter to 'sudo' to preserve some of them, but that won't guarantee that your view is the same. It could be a permissions problem, or a relative path problem or a difference in homes, or a user shell-initialization setting in the other user. You can likely fabricate a good environment on the other side of sudo with something like "sudo env PYTHONSOMETHING=$PYTHONSOMETHING programname" .
So, it's complicated. I'd first use "sudo -i" to get an interactive shell and test what it looks like, and find what to change.
Python uses a different package insallation directory for each user by default.
For example, you can find the location of the keyboard package like so:
>>> import keyboard
>>> keyboard.__file__
'/home/user/.local/lib/python3.8/site-packages/keyboard/__init__.py'
As you can see, it is located in /home/user, which means only user is supposed to use it.
In order to install the package for the root user, just run pip with sudo:
sudo pip3 install keyboard

Creating a scikit-learn contribution package [duplicate]

This question already has answers here:
What does the $ mean when running commands?
(5 answers)
Closed 7 years ago.
As a beginner in Python, I'm reading a book written by Bill Lubanovic
I found something weird.
In that book, After saving simple code in test1.py, which is
print("This standalone program works!")
it says python can run it by typing in
$ python test1.py
However, whenever I try to use that, syntax error happens.
Although I know there are other methods like using exec() which I found in this website, I wanna know why book uses that method which doesn't work at least for me.
It means you need to type everything but the $ in the terminal.
python test1.py
It's just a convention though. Authors also use > python test1.py and other notations.
I don't know which version of his book you're reading, but he mentions it in this version.
In the example that follows, $ is a sample system prompt for you to type a command like python in the terminal window. We’ll use it for the code examples in this book, although your prompt might be different.
You are not supposed to enter the $.
The $ represents the shell/terminal prompt. This is the string of characters that appear in your terminal when it is waiting for input, although $ typically indicates some flavour of unix, e.g. linux.
Your terminal will probably use a different prompt, e.g.
[user#localhost ~]$
Or, if you are using a Windows terminal you might see :
C:\>
or
C:\WINDOWS>
Question was answered in following stackoverflow post:
What does the $ mean when running commands?
What does the $ mean when running commands?
As of now, Python does not implement $ in its syntax. So, it has nothing to do with Python.
Instead, what you are seeing is the terminal prompt of a Unix-based system (Mac, Linux, etc.)
So basically is terminal prompt and you should type in only: python test1.py without $ sign. another example is ~ when using oh-my-zsh.

Run a script from different folder with arguments in python (like in cmd)

I guess I have to use either use os.system or subprocess.call, but I can't figure out how to use it.
I don't have the permission to edit the original folder.
subprocess.Popen('file.py', cwd=dirName) gives me 'The system cannot find the file specified' even though the file clearly exists
If I was typing in cmd,
cd directory
file.py -arg
Edit: Just to be clear I want to run another script using a python script
As you have tagged the question with cmd, I assume that you use Windows. Windows is kind enough to automatically use the appropriate command when you type a document name in cmd, but Python subprocess is not. So you have 2 possible ways here
use shell=True to ask a cmd interpretor to execute the command:
subprocess.Popen('file.py', cwd=dirName, shell=True)
pass explicitely the path of the Python interpretor (or the name if it is in the path)
subprocess.Popen([python_path, 'file.py'], cwd=dirName, shell=True)
At first you need to add the python in windows environment.
Then you can add the file path (the file that you want to run it on command line).
Then you can go to command line page and type the file name and use it like the line below:
FileName commands
for example :
pip install datetime
Its clear you are probably using python 3 rather than python 2. Otherwise you might use os.system as already suggested or commands. Commands is now obsolete as of python 3. To get this working I would use instead:
statusAndOutputText = subprocess.getstatusoutput( os.path.join( dirName, 'file.py' ) )
This will definitely work. I've used it many times in python 3 and it will give you the status in statusAndOutputText[0] and output buffer in statusAndOutputText[1] which are both very useful to have.

Categories