I have a wxPython GUI that needs to run with Python 3 using both Enthought Python (EDM) and Anaconda Python.
The problem is, when using Anaconda Python I have to use pythonw or the script fails with this error:
This program needs access to the screen. Please run with a Framework build of python, and only when you are logged in on the main display of your Mac.
Previously, using Python 2.x, I solved this by specifying:
#/usr/bin/env pythonw
which worked for both Canopy Python 2 and Anaconda Python 2.
However, EDM Python 3 seems to lack pythonw.exe, so the I have to edit the shebang line to
#/usr/bin/env python
to get my script to run in with EDM python 3.
So, is there a way to use the pythonw executable with EDM python 3?
Sure. Just create a script called pythonw and put it in $PATH:
#!/bin/sh
exec /usr/bin/env python "$#"
Related
I have both Python 3 (C:\Python\Python38\python.exe) and Python 2 (C:\Python\Python27\python.exe) installed on my Windows 10 computer, and I want to use Python 2 for a project in VSCode.
However, when I select my interpreter to Python 2.7.1 32--bit and run my code, it still runs in Python 3. Typing python -V in PowerShell or Python window gives me "Python 3.8.2". Both versions have been added to my PATH variables, so I am not quite sure why selecting Python 2 in the VSCode interpreter menu still gives me Python 3.
Has anyone encountered anything similar?
Can you add print(sys.prefix) to check which python interpreter you are using? The python --version you get in the VSCode is not the python interpreter you are using. Python2 works well in my VSCode.
And if you want to distinguish the python2 and python3 clearly. You can rename the python.exe to python2.exe which under the Python2 installation Folder. Then in the Powershell, you can get python3 through the python command and python2 through the python2 command.
In my terminal and in CodeRunner my Python is updated to 2.7.6 but when I ran a shell script in the OSX Automator I found that it is running 2.7.2
How can I update the Automator Python to 2.7.6 like the rest of my compilers ?
I couldn't specify explicitly which python for it to use.
So, I ran it in bash environment with following command:
$ your/python/path /path/to/your/python/script.py
And make sure first line of your python program contains the path to the python environment you wish to use.
Eg:
#! /usr/local/bin/python
The standard python version of ubuntu 13.04 is python 2.7.
I know that I can call a python script of version 3.3 by calling python3.3 or python3 in terminal instead of only "python", which starts the version 2.7...
e.g. python3 myscript.py
But now I have a version 3.3. script in the system start routine and can only tell the path to the file. The system recognizes it as a python script (in the shebang with #!/usr/bin/python3)
But how to open it with the correct version? It is tried to be opened with the standard python install so it wont work nor even show up.
The shebang line #!/usr/bin/python3 should work if sh, bash, etc. is trying to launch your script.
It it is being run from another script as python myscript.py you'll have to find that script and get it to launch the script using python3 myscripy.py
I have installed both Python 2.7.1 and 2.6.5 versions on Windows. I have added only the path of Python 2.6.5 in the Environment Variables.
Now, I want to run a Python script x.py using Python 2.6.5. I know how to do it using the cmd but It would be more convenient to just open it with IDLE and run inside it using the Run Module option.
This is supposedly done by right-clicking over the script, and then going to Edit with IDLE option, but this opens and runs the script using Python 2.7.1. Is there a way to open and run it with Python 2.6.5?
The standard command in the registry for Edit with IDLE is the following:
"C:\Program Files\Python33\pythonw.exe" "C:\Program Files\Python33\Lib\idlelib\idle.pyw" -e "%1"
Now, as you can see, the path is hardcoded into it, so it just cannot use a different Python interpreter like that—at least per default.
However, PEP 397 introduced a new Python launcher for Python making it possible to launch different versions of Python based on the shebang line. So a file starting with #!/usr/bin/env python2 will launch the current Python 2 interpreter, while #!/usr/bin/env python3 will use Python 3.
Using that information, you can launch IDLE for a given Python version dynamically. For example this would edit the file using the launcher’s default Python version:
C:\Windows\pyw.exe -m idlelib.idle -e "%1"
This would force the use of Python 3
C:\Windows\pyw.exe -3 -m idlelib.idle -e "%1"
And this would force the use of Python 2:
C:\Windows\pyw.exe -2 -m idlelib.idle -e "%1"
So what you would need to do is write a script that basically checks which Python version a file wants to be executed with, i.e. by parsing the shebang line manually (sadly the launcher does not give you this information without actually launching the script—yet. I might start a pull request to get such a feature into the core). Then you would run either the Python 2 or Python 3 IDLE using the command above and done.
You would just need to change the Edit with IDLE command to execute your script then and it would work.
A very simple alternative would be to just add another registry key which launches the Python 2 IDLE. So you would have Edit with IDLE and Edit with IDLE (Py2) or something.
To do that, just put the following inside a .reg file and execute it:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)\command]
#="C:\\Windows\\pyw.exe -2 -m idlelib.idle -e \"%1\""
edit: I just noticed that I wrote this mainly about Python 2 vs. Python 3; the launcher ships with Python 2.7 I think, so this will work just the same. You just need to adjust the launcher’s version specificiers to -2.6 and -2.7 or whatever you want.
You can do this with some registry hacks to make IDLE 2.6 your default (rather than 2.7), but that's not really what you want, I think, since then you'd have to reverse the process when you want to test something in 2.7. Unless someone else knows some other way to integrate different IDLE installs into the shell, here are a couple better options:
Open IDLE 2.6 first, and just use the Open File dialog from the GUI.
Use a different IDE that actually supports this functionality. Eclipse with PyDev will let you switch interpreters between runs, or save configurations with different interpreters, and so on.
Can anyone please tell me an IDE for running python programs? Is it possible to run the program through command line?
Take a look at ActiveState's ActivePython. It's quite a nice implementation of Python on Windows. Another way is using Cygwin's Python port. These two are Python implementations. I don't use an IDE, I write my Python code in Notepad++.
To run a python program after saving it to C:\Users\vaibhav\Code\myscript.py:
ActivePython: If I remember right, ActiveState updates the path correctly. So it should be a s simple as:
Press "start" in the task bar
In the search field search for "cmd"
In the appearing box navigate to your folder with the python script: dir Users\vaibhav\Code
call python myscript.py and you're done
Cygwin: After installing Cygwin, you have a full-featured bash terminal on your Windows machine.
click on the Cygwin icon on your desktop
In the appearing window navigate to the folder with your python script: cd /cygdrive/c/Users/vaibhav/Code
type python myscript.py
e voila
IDE for running scripts? You can have any IDE you like, but if you need only to run python scripts you go like this:
python.exe pythonScript.py
I like the EasyEclipse for python distribution. You'd need to have python and java installed of course.
PyDev and Komodo Edit are 2 nice Python IDE on Windows.
I also like the SciTE text editor very much.
These 3 solutions make possible to run Python scripts
I tried to run a Python script with multiprocessing on windows. see this tutorial
It does not work on Windows, but on raspian it went very well. Thus I knew that it was a Windows problem. I installed cygwin and followed this tutorial Installing Python inside Cygwin.
After that I additionally installed numpy with the command easy_install numpy and now i can run python scripts with multiprocessing on windows (from cygwin).