Code
#! C:\Python310\python.exe
import os
import sys
print(f"PY_PYTHON={os.getenv('PY_PYTHON')}")
print(f"{sys.version=}")
print(f"Shebang : '{open(__file__).read().splitlines()[0]}'")
cmd = 'py -0p'
print(cmd)
os.system(cmd)
cmd = f'file {__file__}'
print(cmd)
os.system(cmd)
Output:
λ py t9.py
PY_PYTHON=None
sys.version='3.11.0a7 (main, Apr 5 2022, 21:27:39) [MSC v.1929 64 bit (AMD64)]'
Shebang : '#! C:\Python310\python.exe'
py -0p
-V:3.11 * C:\Python311\python.exe
-V:3.10 C:\Python310\python.exe
-V:3.9 C:\Users\smart\AppData\Local\Programs\Python\Python39\python.exe
file C:\Users\smart\Desktop\budhubot\budhubot\t9.py
C:\Users\smart\Desktop\budhubot\budhubot\t9.py: a C:\Python310\python.exe script, ASCII text executable, with CRLF line terminator
Tried forward slash too.
Edit:
Without space
Shebangs aren't a Windows thing.
On Windows, it must rely on a third-party software: bash, cygwin, python launcher, etc. So you'll find most information about its support in these third-party softwares' documentation.
But first thing, I would try to remove the space between #! and the interpreter's full path. Shebang and interpreter aren't separated by a space usually.
#!C:\Python310\python.exe
Add py.ini file in the py.exe location.
[commands]
py311="C:\Python311\python.exe"6
py310="C:\Python310\python.exe"6
py39="C:\Users\smart\AppData\Local\Programs\Python\Python39\python.exe"6
Note the last char (use any); Without this, pylauncher not works. (2nd outptut). Probably a bug. Repo
Edit:
Shebangs are not working with py -u
Related
I want to run a python file in the Git bash but it is not executing without adding py before the file name. Python is installed but when I use the shortcut to execute the python file, an error is shown as below-
user#WinPC MINGW64 /d/git_basic/scripts (main)
$ python
Python 3.10.0 (tags/v3.10.0:b494f59, Oct 4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
user#WinPC MINGW64 /d/git_basic/scripts (main)
$ ./all_checks.py
Python was not found; run without arguments to install
from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
Please guide me. Thanks
Edit 1
What I want is to run the python file in the terminal without using python or py in the command (py file.py or python file.py). I want to run it like this ./file.py
This is the all_checks.py code-
#! /usr/bin/env python3
import os
import sys
def check_reboot():
# returns true if pc has a pending reboot
return os.path.exists("/run/reboot-required")
def main():
if check_reboot():
print("pending reboot.")
sys.exit(1)
print("No pending reboot.")
sys.exit(0)
main()
Edit 2
I was able to run the file with ./ by just changing the python3 to python in the shebang line.
I was able to run the file with ./ by just changing the python3 to python in the shebang line.
Python 3 Docs - Shebang lines
I was not clear on exactly what you wanted but I have 2 options here.
Just running the file and making the file an executable.
If you just want to run it then use the terminal command, python file-name or python3 file-name if you have any python 2 installations.
You do not run a python file with this command: ./file-name
However, if you want to make it an executable file keep on reading.
If you are trying to make an executable, pyinstaller will do the job for you. If you have pip, then run the command pip install pyinstaller. Then, once you have pyinstaller installed, open terminal or command prompt and locate the directory of the python file using cd. Then once you are in the directory in terminal/command prompt run the command pyinstaller --onefile file-name include -w after --onefile if you do not want the command prompt/terminal/command-line to open up when run.
(Pyinstaller works for all operating systems)
I am trying to import pyodbc module on a windows computer. It works in the terminal, but not the IDLE. The error message in IDLE is:
Traceback (most recent call last):
File "FilePath/Filename.py", line 3, in <module>
import pyodbc
ImportError: No module named pyodbc
This typically occurs when multiple versions of python are installed with different paths. You can check to see if you have multiple installations by opening up the IDLE terminal and using
import sys
sys.version
sys.path
These commands will print the system PATH and version of the current instance of python. Use this in both IDLE and the command line terminal to see where each differ. Once you know which version is the one you want then just remove the other. You could also remove all python instances and then reinstall a clean python environment but then you would have to re-install all of your modules using pip or easy_install
Open python in cmd (type python and press enter)
Import the module in cmd (type import modulename)
Type modulename.__file__
You will get the path where the module is stored
Copy the corresponding folder
In IDLE, import sys and typing sys.executable to get the paths where it looks for modules to import
Paste your module's folder in the path where IDLE looks for modules.
This method worked for me.
You can pip show after install package and know about location where package installed.
After that check in IDLE sys.path and if directory with package not in sys.path try to add it.
import sys
sys.path.append("/home/dm/.local/lib/python3.6/site-packages")
# or another folder that `pip show` about package.
this happen because of multiple python installed (32bit version, 64bit version) or 3v and 2.7v so to solve this problem you have to invoke the idle for that specific version like this
cd to the dir of the version that the import work fine in cmd in that folder type this command below
pythonw.exe Lib\idlelib\idle.pyw
this command will invoke idle for that version
and the import will work fine
Me too had the same issue while trying to import a module which was successfully imported on terminal and not able to install on IDLE.
How I fixed?
Assuming you know how to execute commands on terminal as well as inside of python interpreter
Open your Terminal & execute the below commands :
:~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>> import sys
>>> sys.version
'3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]'
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-
packages', '/usr/lib/python3/dist-packages']
>>>
Now import your module inside of your python3 interpreter.
>>> import nester
>>>
>>> nester.__file__
'/usr/local/lib/python3.6/dist-packages/nester.py'
>>>
Open your IDLE and run the below commands and compare them
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more
information.
>>> import sys
>>> sys.version
'3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit
(AMD64)]'
>>> sys.path
['','C:\Users\username\AppData\Local\Programs\Python\Python39\Lib\idlelib', 'C:\Users\username\AppData\Local\Programs\Python\Python39\python39.zip', 'C:\Users\username\AppData\Local\Programs\Python\Python39\DLLs', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib', 'C:\Users\username\AppData\Local\Programs\Python\Python39', 'C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages']
>>> sys.executable
'C:\Users\username\AppData\Local\Programs\Python\Python39\pythonw.exe'
Now if you compare both outputs from Terminal & IDLE,
Terminal Module location is different from IDLE
I was using Ubuntu 18 terminal on windows machine
So I just copied my file to 'C' directory and ensured its file privileges. That's it.
:~$ cp -p /usr/local/lib/python3.6/dist-packages/nester.py /mnt/c/Users/username/AppData/Local/Programs/Python/Python39/Lib/
It worked!!
I Found the solution. It works for me
The problem is your installation directory does not match with the python version directory.
solution is >>>
type %localappdata% in your search bar then go to this folder.
here select the program folder. then select Programs , Python , Python version , Scripts
copy the location of the Scripts folder
open command prompt and type cd //yourpath (in my case cd C:\Users\3C HOUSE\AppData\Local\Programs\Python\Python37\Scripts)
if you wanna install numpy , now run pip install numpy
When you put your python scripts that have import pandas in the same folder as the site packages like pandas for example and use the same version of python that is used on CMD, it should help run your scripts in IDLE.
Check the path of your code, and that of the module. Copying the module to the path where code is worked for me.
'sys.executable' will give the path where code is stored.
For windows, open command prompt and enter pip show pyodbc to get the path of package and copy the path.
then open idle and run these lines
import sys
sys.path
Match the path from command prompt and the paths mentioned in the list provided by running above lines in IDLE. If the path is not mentioned then run these lines in idle
sys.path.append("Enter the copied path of package here")
After executing these lines, check again by importing the package that if it works for you.
Hi My python installation is in different directory and i am using a docker image which is mac based and it is referring shebang line as /user/local/bin/python3 from other folder in shell script .
my python installation path
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/myuser/project', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
>>>
so is there a way without changing the shebang line i can redirect or link to my installation of python3 to get out of this error.
is it recommended to install python3 in given path. ?
please advice.
If you can't modify the shebang of the file, and you have access to the Dockerfile that creates your docker image, you can add a command directive to create a symbolic link: ln -s /usr/bin/python3 /usr/local/bin/.
If you don't have access to the Dockerfile. Then you can run the above command from within the running docker instance. That should solve your issue without having to modify the file.
https://docs.docker.com/engine/reference/builder/#cmd
You could set you shebang to "/usr/bin/env python" as usual, then set your path appropriately so that the correct version of python is on the path for your executable. In bash you can set the path on the command line using:
PATH=python/path:$PATH app
I will sometimes ignore the shebang and type python/path/python $(which app) in order to control which python interpreter is running.
There’s a project where I need to use both Python 3.3 and 2.7. I am trying to launch a script under Python 2.7 but it’s not working. Here is a simple example.
first.py
import subprocess
import sys
print('Inside first.py')
print(sys.version)
subprocess.Popen(["C:\Python27\ArcGISx6410.2\Python.exe", "second.py"])
second.py
import arcpy
print 'This is second.py'
This doesn’t work and the output is
Inside first.py
3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)]
File "C:\Python33\lib\site.py", line 173
file=sys.stderr)
^
SyntaxError: invalid syntax
That’s the entire stack trace. If I were to replace C:\...Python.exe with notepad.exe then it works. I’m using Liclipse on Windows 7.
UPDATE: it appears different versions of Python are run, when from the command line python first.py is 3.3 but py first.py or just first.py then 2.7 is used.
Try:
import os
subprocess.Popen(["C:\\Python27\\ArcGISx6410.2\\Python.exe", "second.py"], env=dict(os.environ, PYTHONHOME="C:\\Python27\\ArcGISx6410.2"))
Python on Windows needs a little help sometimes to figure out which version of the standard library to use.
I'm trying to install Anaconda on my Windows 7 machine. I often use cygwin to for my command-line work, and I would like to manage Anaconda from there. I've worked through the graphic installer without any issues, and checked necessary boxes to reset my default path to this install of python. I go ahead to check where python is and initially I get this...
$ which python
/usr/bin/python
From here python works fine...
$ python
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
But I'm trying to work from anaconda, so I should just need to redefine my path...
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
And now I should be good to go, but when I try and step into python, it just hangs
$ python
Any idea why this might be happening? verbose return, below...
$ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# C:\anaconda\lib\site.pyc matches C:\anaconda\lib\site.py
import site # precompiled from C:\anaconda\lib\site.pyc
# C:\anaconda\lib\os.pyc matches C:\anaconda\lib\os.py
import os # precompiled from C:\anaconda\lib\os.pyc
import errno # builtin
import nt # builtin
# C:\anaconda\lib\ntpath.pyc matches C:\anaconda\lib\ntpath.py
import ntpath # precompiled from C:\anaconda\lib\ntpath.pyc
# C:\anaconda\lib\stat.pyc matches C:\anaconda\lib\stat.py
import stat # precompiled from C:\anaconda\lib\stat.pyc
# C:\anaconda\lib\genericpath.pyc matches C:\anaconda\lib\genericpath.py
import genericpath # precompiled from C:\anaconda\lib\genericpath.pyc
# C:\anaconda\lib\warnings.pyc matches C:\anaconda\lib\warnings.py
import warnings # precompiled from C:\anaconda\lib\warnings.pyc
# C:\anaconda\lib\linecache.pyc matches C:\anaconda\lib\linecache.py
import linecache # precompiled from C:\anaconda\lib\linecache.pyc
# C:\anaconda\lib\types.pyc matches C:\anaconda\lib\types.py
import types # precompiled from C:\anaconda\lib\types.pyc
# C:\anaconda\lib\UserDict.pyc matches C:\anaconda\lib\UserDict.py
import UserDict # precompiled from C:\anaconda\lib\UserDict.pyc
# C:\anaconda\lib\_abcoll.pyc matches C:\anaconda\lib\_abcoll.py
import _abcoll # precompiled from C:\anaconda\lib\_abcoll.pyc
# C:\anaconda\lib\abc.pyc matches C:\anaconda\lib\abc.py
import abc # precompiled from C:\anaconda\lib\abc.pyc
# C:\anaconda\lib\_weakrefset.pyc matches C:\anaconda\lib\_weakrefset.py
import _weakrefset # precompiled from C:\anaconda\lib\_weakrefset.pyc
import _weakref # builtin
# C:\anaconda\lib\copy_reg.pyc matches C:\anaconda\lib\copy_reg.py
import copy_reg # precompiled from C:\anaconda\lib\copy_reg.pyc
# C:\anaconda\lib\traceback.pyc matches C:\anaconda\lib\traceback.py
import traceback # precompiled from C:\anaconda\lib\traceback.pyc
# C:\anaconda\lib\sysconfig.pyc matches C:\anaconda\lib\sysconfig.py
import sysconfig # precompiled from C:\anaconda\lib\sysconfig.pyc
# C:\anaconda\lib\re.pyc matches C:\anaconda\lib\re.py
import re # precompiled from C:\anaconda\lib\re.pyc
# C:\anaconda\lib\sre_compile.pyc matches C:\anaconda\lib\sre_compile.py
import sre_compile # precompiled from C:\anaconda\lib\sre_compile.pyc
import _sre # builtin
# C:\anaconda\lib\sre_parse.pyc matches C:\anaconda\lib\sre_parse.py
import sre_parse # precompiled from C:\anaconda\lib\sre_parse.pyc
# C:\anaconda\lib\sre_constants.pyc matches C:\anaconda\lib\sre_constants.py
import sre_constants # precompiled from C:\anaconda\lib\sre_constants.pyc
# C:\anaconda\lib\locale.pyc matches C:\anaconda\lib\locale.py
import locale # precompiled from C:\anaconda\lib\locale.pyc
import encodings # directory C:\anaconda\lib\encodings
# C:\anaconda\lib\encodings\__init__.pyc matches C:\anaconda\lib\encodings\__init__.py
import encodings # precompiled from C:\anaconda\lib\encodings\__init__.pyc
# C:\anaconda\lib\codecs.pyc matches C:\anaconda\lib\codecs.py
import codecs # precompiled from C:\anaconda\lib\codecs.pyc
import _codecs # builtin
# C:\anaconda\lib\encodings\aliases.pyc matches C:\anaconda\lib\encodings\aliases.py
import encodings.aliases # precompiled from C:\anaconda\lib\encodings\aliases.pyc
import operator # builtin
# C:\anaconda\lib\functools.pyc matches C:\anaconda\lib\functools.py
import functools # precompiled from C:\anaconda\lib\functools.pyc
import _functools # builtin
import _locale # builtin
# C:\anaconda\lib\encodings\cp1252.pyc matches C:\anaconda\lib\encodings\cp1252.py
import encodings.cp1252 # precompiled from C:\anaconda\lib\encodings\cp1252.pyc
# zipimport: found 13 names in C:\anaconda\lib\site-packages\runipy-0.1.0-py2.7.egg
# zipimport: found 144 names in C:\anaconda\lib\site-packages\setuptools-3.6-py2.7.egg
Python 2.7.7 |Anaconda 2.0.1 (64-bit)| (default, Jun 11 2014, 10:40:02) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and htt
Another (possibly related) issue I'm having is needing to reset the path every time I close/open cygwin. I've entered the following text into .bashrc and .profile to try and set the path permanently:
# Set path to python from anaconda install
export PATH=/cygdrive/c/anaconda:$PATH
After opening and closing cygwin, I return to:
$ which python
/usr/bin/python
Could this be related to setting certain system environment variables?
To work with the interactive Python shell in Cygwin I use the -i option.
To get it from the Anaconda install, I used the steps suggested above:
$ export PATH=/cygdrive/c/anaconda:$PATH
$ which python
/cygdrive/c/anaconda/python
Then I launch python within Cygwin with the -i option:
$ python -i
Python 2.7.8 |Anaconda 2.1.0 (64-bit)| (default, Jul 2 2014, 15:12:11) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://binstar.org
>>>>
The libraries are also working fine. For instance the pandas library (which has been installed through Anaconda) can be imported now.
>>>> import pandas
>>>> pandas.DataFrame
<class 'pandas.core.frame.DataFrame'>
Now to make this permanent I added the path in my bashrc file:
$ echo 'export PATH=/cygdrive/c/anaconda:$PATH' >> .bashrc
$ source .bashrc
$ which python
/cygdrive/c/anaconda/python
I hope this helps.
I too was having an issue getting anaconda set up with my Cygwin / Windows 7 system. What worked was the following:
Edited the ~/.bashrc. Add below to the bottom of file.
export PATH=/cygdrive/c/Anaconda:$PATH
This mapped Cygwin's python to the anaconda distribution as proof by entering which python in Cygwin's console. However, when launching python it would hang up forcing me to ^C out of the command. I found that python -i will launch the interactive python interpreter with no issues.
Path
Regarding your path issue, see Installing anaconda to use with windows on how to fix that. Alternatively, you can just run /cygdrive/c/Anaconda/python.exe to start the Anaconda/windows python and leave the standard python pointing to the cygwin version.
Note that few of the suggestions given on the internet also include the necessary change to PYTHONPATH: if you use the anaconda/windows python, you also want the anaconda python libraries. Binary modules like numpy will surely crash or misbehave if you mix things up. The easiest way to check this is to install a module in cygwin that you do not need in Conda and test whether you can import that from anaconda python. If you can, your path is wrong.
Anaconda python will automatically add c:\Anconda to your PYTHONPATH, but if you have it set to serve CYGWIN python libraries, those will be found before the Anaconda libs. Try
import sys
sys.path
Terminal
However, this is also a terminal issue: Anaconda (Windows) python expects a windows shell and a modern cygwin shell is like an xterm, so very different. Diagnose this by running python --help, if that works but plain python hangs, the interactive prompt is the problem. This means you can run python programs, you just cannot interact with them.
A way around this is to use the cygwin bash shell instead of the cygwin terminal (both are under Cygwin group in the Start menu). They look very similar, but the font in the bash shell is less pretty and you cannot make the window wider than 80 chars (like all other CMD windows, never understood that 'feature'). This is cygwin bash running inside a CMD window, and your anaconda python will be interactive. The cygwin terminal gives you a bash shell running in a putty derivative, which mimics an xterm and cannot be used by anaconda python.
There is another problem with conda's interpretation of cygwin. When you source the activation script for an environment, it prepends e.g.
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/mingw-w64/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/usr/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Library/bin
/C/Users/Thomas.Harris/AppData/Local/Continuum/Anaconda3/envs/blah/Scripts
to your PATH. However, these are not proper directories in cygwin.
I fixed this with
ln -s /cygdrive/c /C
and then if you create conda environments with specific versions of python they should work.
Regarding setting your path permanently, edit the .profile in your home directory (it's a hidden file).
.profile (other names are also valid, see the bash man page) contains bash commands. It is executed when bash is started as login shell.
At the bottom of this file, put your set path command:
export PATH=/cygdrive/c/anaconda:$PATH
As for why python is hanging, please provide more information - for example what's the output of python -v?
I'm a little late in coming to this, but I was having the same problem as you #thomasshouler. Try adding the export line below to the end of the .bash_profile and .profile files as well as your .bashrc file (as mentioned above). Make sure to "source .bash_profile" etc for each file.
export PATH=/cygdrive/c/anaconda:$PATH
Once the Cygwin path is updated as mentioned above, you can also add an alias in the .bashrc file:
alias python='python -i'
This will bring up the Anaconda python interpreter in interactive mode from a Cygwin bash prompt
Found that for Anaconda 4, do:
vim /home/[Username]/.bash_profile
Append to the last line:
PATH=$PATH:/cygdrive/c/Users/[Username]/Anaconda3/Scripts/
This solution worked for me as the usual anaconda3/bin could not be found.