How do you run a Python script in a newer Python interpreter? - python

I am using several applications that are using different versions of Python:
Nuke - 2.7
3Dequalizer - 2.6
linux - 2.6.6
I am getting various problems trying to get them all to communicate with one another, so I was wondering if it's possible to change Python interpreter during a script.
E.g. Start in 2.6, then run a Python script in 2.7 from a script in 2.6
EDIT:
nuke_install = "/path/to/nuke"
cmd = nukeLauncher + " -t"
os.system(cmd)
The -t flag allows for nuke to be run without a GUI. This code works when run in a Python interpreter, but when I run via a Python script in 3dequalizer it gives me the:
ImportError: No module named site
To add another level of confusion, I can import site inside 3dequalizer. The sys.path for 3dequalizer contains the same paths as when run directly from the interpreter, with a few additions for the python lib that comes with 3de.
Also PYTHONPATH is empty inside 3dequalizer. Does this matter if sys.path is pointing to the right paths?

I am not sure it is really the way to go; but if you really want to do it, you could use the os.system command with somthing like:
os.system("python2.7 myscript.py")
which will execute the program python2.7 (as long as it is in your executable path) with the name of the script as its argument (before returning to the current) statement in your initial script.
But honestly, I think you should do it in some other way. Regards.

Related

Is there a way to execute .py scripts from path in windows?

I would like to use a python script anywhere within command prompt. This is possible in unix, but I couldn't find anything for windows. Do I have to convert it to .exe?
Edit: not sure why this is being downvoted, maybe it's a silly question but I can't find any similar threads here and I can't be the first person to want to execute .py scripts from their path...
Edit 2: I think my wording was unclear. I would like to know a method to execute python scripts in Windows without needing to specify python path/to/script.py every time. Here is a solution on Linux where the shebang statement invokes the python interpreter, and the script in question can be easily placed in bin: How do I install a script to run anywhere from the command line? . Does there exist a solution like this for Windows?
Here's a solution for running myScript.py:
add to the myScript.py file a first line #!python (or #!python3 if you want to use Python 3)
For instance:
#!python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
change the "opens with" property of myScript.py to py.exe (to find where it is use where py-- I have it in C:\Windows\py.exe)
put the script myScript.py somewhere in your Windows path
and now you should be able to type myScript.py anywhere in a command prompt and run the Python script with your chosen Python version.
See: https://docs.python.org/3.6/using/windows.html#from-the-command-line

How can I run the Python3 interpreter with variables/references/etc as defined in a python file?

I'm new to python and enjoying learning the language. I like using the interpreter in real time, but I still don't understand completely how it works. I would like to be able to define my environment with variables, imports, functions and all the rest then run the interpreter with those already prepared. When I run my files (using PyCharm, Python 3.6) they just execute and exit.
Is there some line to put in my .py files like a main function that will invoke the interpreter? Is there a way to run my .py files from the interpreter where I can continue to call functions and declare variables?
I understand this is a total newbie question, but please explain how to do this or why I'm completely not getting it.
I think you're asking three separate things, but we'll count it as one question since it's not obvious that they are different things:
1. Customize the interactive interpreter
I would like to be able to define my environment with variables, imports, functions and all the rest then run the interpreter with those already prepared.
To customize the environment of your interactive interpreter, define the environment variable PYTHONSTARTUP. How you do that depends on your OS. It should be set to the pathname of a file (use an absolute path), whose commands will be executed before you get your prompt. This answer (found by Tobias) shows you how. This is suitable if there is a fixed set of initializations you would always like to do.
2. Drop to the interactive prompt after running a script
When I run my files (using PyCharm, Python 3.6) they just execute and exit.
From the command line, you can execute a python script with python -i scriptname.py and you'll get an interactive prompt after the script is finished. Note that in this case, PYTHONSTARTUP is ignored: It is not a good idea for scripts to run in a customized environment without explicit action.
3. Call your scripts from the interpreter, or from another script.
Is there a way to run my .py files from the interpreter where I can continue to call functions and declare variables?
If you have a file myscript.py, you can type import myscript in the interactive Python prompt, or put the same in another script, and your script will be executed. Your environment will then have a new module, myscript. You could use the following variant to import your custom definitions on demand (assuming a file myconfig.py where Python can find it):
from myconfig import *
Again, this is not generally a good idea; your programs should explicitly declare all their dependencies by using specific imports at the top.
You can achieve the result you intend by doing this:
Write a Python file with all the imports you want.
Call your script as python -i myscript.py.
Calling with -i runs the script then drops you into the interpreter session with all of those imports, etc. already executed.
If you want to save yourself the effort of calling Python that way every time, add this to your .bashrc file:
alias python='python -i /Users/yourname/whatever/the/path/is/myscript.py'
You set the environment variable PYTHONSTARTUP as suggested in this answer:
https://stackoverflow.com/a/11124610/1781434

gud-pdb emacs usage and PATH

I'm trying to use gud-pdb for Python debugging within Emacs.
Having an issue that pdb doesn't seem to be searching the PATH when looking for my .py files
I.e., I have a python script in a dir which is on the PATH, I can run this script from anywhere outside of pdb, i.e., from the command line.
But when I try and run it from within pdb it tells me the file doesn't exist.
I'm trying to run pdb against the script in a dir that contains the data to be processed.
I think this is a standard thing to want to do. I successfully do it for gdb and C programs all the time.
Anyone had this issue and know how to fix it?
Given that you're able to run your script outside of Emacs, but not
within, you probably
need
exec-path-from-shell.
This syncs up environment variables (like PATH) between your shell
and Emacs.
Have you tried the realgud package since you are using python?
;;M-x load-library realgud python -m pdb myscript.py
(package-install 'realgud) ;; python debugging in emacs
(defun sdev/init-realgud
(interactive)
(load-library "realgud"))
(sdev/init-realgud 1)

Run a a script from system python interpreter from an external python interpreter

XBMC has it's own python interpreter inside of it.
From this built in interpreter I need to run a script on the local machine python(i.e. the system python).
I call os.system("python myScript.py") but I get back the error in my system error:
ImportError: No module named site
I was hoping for ideas of guidance on how to troubleshoot this issue.
Is it even possible to do?
I'm thinking it has something to do with the pythonpath/pythonhome variable.
If you can locate your XBMC python interpreter's path (I would imagine it has the same python and is located inside xbmc/bin/ or something similar), you could run that python version instead of the default one when running python.
Your code should like so:
os.system(python_fullpath + " " script_fullpath)
Where both python_fullpath and script_fullpath, as the names imply, are full paths to those files.
Like, for example:
python_fullpath = "C:\Program Files\XBMC\bin\python.exe"
script_fullpath = "C:\Users\myuser\Desktop\myScript.py"

importing the wx module in python

When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.

Categories

Resources