I was wondering if there is a way to automatically run commands on entering the python shell as you would with the .bash_profile or .profile scripts with bash. I would like to automatically import some modules so I don't have to type the whole shebang everytime I hop into the shell.
Thanks,
Yup you can use the PYTHONSTARTUP environment variable to do this as outlined here
Also consider using ipython if you're doing a lot of interactive work. Your options for this kind of automation expand significantly.
Related
I'm writing some python to render stuff that I tweak and run a lot, and that runs inside a virtual env. I would like a keyboard command to run a bash script (that launches python) inside the known terminal and virtual env.
I played a bit with setting up a shell script and a custom task, but entering the virtual env is always a bit tricky.
I don't need a debugger or anything complicated, just a way to run the python code and attach a keystroke to it.
https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
https://code.visualstudio.com/docs/python/jupyter-support-py
You can just run the python.exe/python binary in the virtualenv folder as an executable.
So to run a specific python file with a virtual environment:
For Windows it would be .\path_to_virtualenv\Scripts\python.exe yourfile.py, and for Unix system it would be ./path_to_virtualenv/bin/python yourfile.py
And instead of running a python file, you can probably pipe the input command into the python executable in the path above if you want to run a specific python command.
So something like COMMAND | ./path_to_virtualenv/bin/python yourfile.py
Could you please explain the difference between the way you want and the following operation:
Use "Ctrl+Shift+P" and type "Python: Select Interpreter" (the same as click the interpreter in the lower right corner which is on the right side of python).
Use "Run Python".
I think there is no difference because before your using a keyboard command, you still need to choose the interpreter.
By the way, use jupyter notebook or interactive window will be a good chioce as well.
To start off, I am a complete noob to Debian, Python, and shell scripts, so please speak to me like I am a toddler.
I have a python script I am running through a virtualenv, and I want to execute it via a shell script. Here is what I'm typing in to the terminal to do so:
source .profile
workon cv
cd Desktop/Camera
python main.py
I tried turning this into a shell script, but I receive the error -- source: not found
I've found no answer to my problem, at least not in any terms I can understand. Any advice would be appreciated. Furthermore, before you answer, I also have no idea why it is I need to execute source .profile, I'm simply following a beginner guide for the project which can be found here: https://www.hackster.io/hackerhouse/smart-security-camera-90d7bd
Thanks in advance, and sorry if this is a dumb question.
Best practice for Shell would be shebang at the begging like #Veda suggested.
Execute the shell script using bash like bash shell.sh as the link suggests using relative locations rather than absolute ones
Add a hashbang at the top of your script (should be the first line):
#!/bin/bash
This will ensure you are running your shell-script in bash. Having a shell does not mean it's bash. Not all shells have the source function that you are using (bash has it).
Some prefer the following:
#!/usr/bin/env bash
Since you are a "beginner" I think it does not really matter. The first makes sure it's using the bash in /bin, the second is using the PATH variable to find bash, so the user of your script can provide it's own bash if he/she wants.
I am starting to learn python with sublime and ipython. They are cool tools and but I want a way to connect them.
I normally have a sublime and a IPython console open. Is there any command that I can run in sublime just send:
runfile('~\someExample.py', wdir='~\myDir')
to the running IPython console?
Thanks!
I edit in geany and use, in ipthon:
run myfile
to load and run myfile.py. ls and cd and pwd are available to check and change the directory.
I save the file from the editor, but I control the run from the ipython console.
If I just need to run the script, I could do a python myfile.py in the editor's terminal window, or maybe via the editor's execute shortcut. But the value in running the code in an existing ipython console is that I can interact with the newly loaded code and variables. I can examine variables, rerun functions with new values, etc.
There is a plugin to do this called SendCode; you can install it directly through the package manager.
As another answer mentioned, you can run some set of shell commands from within ipython, including execution of a file with run python_file.py, as well as other shell commands including cd, mkdir, rm, mv, etc.
I'm looking to have the same functionality (history, ...) as when you simply type python in your terminal. The script I have goes through a bunch of setup code, and when ready, the user should have a command prompt. What would be the best way to achieve this?
Either use readline and code the shell behaviour yourself, or simply prepare the environment and drop into IPython.
Run the script from the console with python -i. It will go through the commands and drop you in the usual Python console when it's done.
How do I set, temporarily, the PYTHONPATH environment variable just before executing a Python script?
In *nix, I can do this:
$ PYTHONPATH='.' python scripts/doit.py
In Windows, this syntax does not work, of course. What is the equivalent, though?
How temporarily? If you open a Windows console (cmd.exe), typing:
set PYTHONPATH=.
will change PYTHONPATH for that console only and any child processes created from it. Any python scripts run from this console will use the new PYTHONPATH value. Close the console and the change will be forgotten.
To set and restore an environment variable on Windows' command line requires an unfortunately "somewhat torturous" approach...:
SET SAVE=%PYTHONPATH%
SET PYTHONPATH=.
python scripts/doit.py
SET PYTHONPATH=%SAVE%
You could use a little auxiliary Python script to make it less painful, e.g.
import os
import sys
import subprocess
for i, a in enumerate(sys.argv[1:]):
if '=' not in a: break
name, _, value = a.partition('=')
os.environ[name] = value
sys.exit(subprocess.call(sys.argv[i:]))
to be called as, e.g.,
python withenv.py PYTHONPATH=. python scripts/doit.py
(I've coded it so it works for any subprocess, not just a Python script -- if you only care about Python scripts you could omit the second python in the cal and put 'python' in sys.argv[i-1] in the code, then use sys.argv[i-1:] as the argument for subprocess.call).
In Windows, you can set PYTHONPATH as an environment variable, which has a GUI front end. On most versions of Windows, you can launch by right click on My Computer and right click Properties.
You use SET on Windows:
SET PYTHONPATH=.
python scripts/doit.py
Windows can localize variables within the script. This will save having to set PYTHONPATH before running. It will also help when different scripts require different conflicting PYTHONPATHs.
setlocal
set PYTHONPATH=.
python.exe scripts\doit.py
endlocal
For more info, check MS documentation
setlocal
endlocal