I have a shortcut which runs python3 in a terminal window. I would like to add some import commands to a python script which is to be run when python starts.
How can I do this?
eg; I have xfce4-terminal -e python3 which starts a graphical terminal session with python3 running. I want to add something to this to make python3 execute a script, however I do not want python to exit at the end of the script, which is the default behaviour if a filename is given immediatly following the python3 command.
See python --help. It mentions an environment variable called PYTHONSTARTUP which looks like it could help you get where you want.
Related
I want to create a makefile target in Ubuntun to spawn a poetry shell. Here are the things I want to do if I were in the command shell:
type poetry shell, which is going to spawn a shell within the virtual environment.
do something in the poetry shell, such as executing a python script using command python ...
To facilitate the process, I want to create a makefile looking something like below
# if I can set SHELL in a specific way
# SHELL = ?????
foo:
poetry shell
echo "Success"
# many lines to be executed in the poetry shell, here is an example
python <a_python_file>
The problem as I found is that the execution will be hanging after poetry shell and will not execute echo "Success"
I know this could be a general question on spawning a shell from command shell, and it is not limited to poetry. Any comments/suggestions would be appreciated.
As a comment pointed out, what I really want is python ... instead of poetry run python .... I edited it.
As a comment pointed out, I added some pseudo code in the makefile.
I think there's some misunderstanding. I'd never heard of poetry before but a quick look at its manual makes clear how it works.
If you run poetry shell then you get an interactive shell which you are expected to type commands into from your keyboard. The reason it "hangs" is that it started a poetry shell and is now waiting for you to enter commands. It's not hung, it's waiting for some input.
You don't want to run an interactive set of poetry commands, you have a predefined set of poetry commands you want to run. For that, you would use poetry run as mentioned in the comments above:
foo:
poetry run python <first command>
poetry run python <second command>
...
echo "Success"
If you want to run all the commands within a single instance of poetry, you have to combine them all into a single invocation, maybe something like this (I didn't try this so the quoting might be wrong):
foo:
poetry run 'python <first command> && python <second command> ...'
echo "Success"
You could do this:
foo:
poetry run $(MAKE) in-poetry
echo "Success"
in-poetry:
python <command1>
python <command2>
Now if you run make foo all the commands in the in-poetry target are run within the poetry environment, because poetry run runs a make program in its environment, and that make program runs a bunch of python.
But if someone ran make in-poetry directory (not via the foo target) then those python operations would not be run inside a poetry environment (unless the user set it up before they ran make).
How do I run a python script (with imported modules) from Applescript?
From terminal, these scripts work fine
python -m api.get_classrooms
python /absolute/path/to/project/api/get_classrooms.py
If I run the terminal script from AppleScript, I get module not found errors:
set pythonPath to "/path/to/anaconda3/envs/jupyter/bin/python"
set scriptPath to "/absolute/path/to/project/api/get_classrooms.py"
set result to do shell script pythonPath & " " & scriptPath & " "
I get the error
ModuleNotFoundError: No module named 'api'"
I did add my project to the $PYTHONPATH within .bash_profile
echo $PYTHONPATH => /path/to/project/a
And the python file (called by AppleScript) is set to executable
Is Applescript not aware of the PYTHONPATH?
Any push in the right direction is appreciated.
AppleScript's do shell script does not import the environment used by a typical interactive shell. This includes the $PATH variable, which I suspect is where your pro blew lies. You can coerce it into doing a full setup by calling the path_helper utility that apps like Terminal use. Try running this code:
set pythonPath to "/path/to/anaconda3/envs/jupyter/bin/python"
set scriptPath to "/absolute/path/to/project/api/get_classrooms.py"
set result to do shell script "eval `/usr/libexec/path_helper -s`; python -m api.get_classrooms"
Thanks to ted wrigley :) I was finally able to get it to work...
By not targeting the get_classrooms.py script directly.
Instead, AppleScript runs the below shell script (working within the project's subdirectory 'bash'. The bash script executes the python script. It wasn't the exact solution I wanted...but it works...and is (perhaps) more elegant....at least until the project is converted to a webapp with flask.
For anyone having trouble targeting python scripts with applescript applescript=>bash=>python
Applescript
do shell script "/Users/me/path/to/project/bash/applescript.sh"
Bash script within project/bash directory
#!/usr/bin/env bash
# eval /usr/libexec/path_helper -s; # didn't need
export PYTHONPATH='/Users/me/path/to/project';
/Users/me/anaconda3/envs/jupyter/bin/python -m api.get_classrooms
I am currently using crontab to run a SH script at boot which navigates to the path of my python script, switches to a different python environment and runs my python script, although it works perfectly fine it runs hidden without a terminal for me to monitor whatever the python interpreter prints like errors, how could I make it so the python interpreter points at a newly opened terminal window?
Here is my SH script (runs with the bash interpreter, not sh):
#!/bin/sh
cd /
cd /home/pi/Desktop/Juvia-py
source defenv/bin/activate
python3 juvia.py &
and my crontab entry:
#reboot bash /home/pi/launcher.sh
Thank you
If you just want to record errors, you could pipe STDOUT and STDERR to files, something like
python3 juvia.py >stdout.log 2>stderr.log &
But if you wanted to open it in a separate window so you could interact you would need to manage STDIN more creatively.
I was hoping for some advice on using the subprocess module.
I'm trying to run a bash job within a python script so my bash command (in the right directory) is: ./program myjob.inp
This is just running the executable "program" with myjob.inp being the input file (and my python script constantly updates myjob.inp).
I know that if I just wanted to run "program", I could do something like:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program"], stdout = fstore_tmp)
However, I can't figure out how to run the job taking in the input file myjob.inp such that it's doing the equivalent of ./program myjob.inp. I tried:
with open("tmp.dat","w") as fstore_tmp:
subprocess.call(["./program", "myjob.inp"], stdout = fstore_tmp)
However, that doesn't seem to be working. Does anyone have any suggestions? Thanks!
I am using anaconda python. So every time, in my mac terminal, I input the terminal command:
source /Users/mylaptop/anaconda/bin/activate /Users/mylaptop/anaconda
And then I activated the anaconda python environment. But I don't want to write this command line every time, so I tried a bash script like this:
#! /bin/bash
source /Users/mylaptop/anaconda/bin/activate /Users/mylaptop/anaconda
and I put this file in the directory /usr/local/bin. But unfortunately, I cannot log into anaconda environment in this way. There is no error message showed up in the terminal. So I do not know what is happening here.
Could anyone help me out?
The easiest fix is to just put /Users/mylaptop/anaconda in your PATH, by adding something like
export PATH="/Users/mylaptop/anaconda:$PATH"
to your bash profile (~/.profile).
You can't put the activate script in a script because it has to be "sourced" to work. source causes the script to be run in your current shell (as opposed to a subshell, which is how the bash script you wrote is run). This is necessary because it modifies your PATH environment variable, and environment variables from your current shell cannot be modified by subshells.