How can I use my windows Python environment on Cygwin? - python

I have several packages on my windows python environment and I would like to use them inside Cygwin. Can I setup a virtual environment linked to the files used for the windows' python or do I have to copy the files?

Older Cygwin was written with its stdin, stdout, stderr bound to Windows i.e. it was a Win32 console program. Then it was possible to run Windows version of Python as:
/cygdrive/c/Python27/python.exe
You could even have added /cygdrive/.../Pythonxx to your path variable.
Newer versions are written with implemented their own terminals and when you run an Win32 console app like Python it freezes.
Even running it under "cmd" run inside cygwin won't work.
Half an answer is to run it always in interactive mode, then it works a little. Eg.
/cygdrive/c/Python27/python.exe -i program.py
or just for shell
/cygdrive/c/Python27/python.exe -i
You can try playing around under cygwins options, and change a terminal which it currently emulates, to see whether it will do you any good.
Running Windows version of Python under cygwin, if you have older one, will be like you are still in Windows but using Unix console.
I think that is not something you would like to do anyway,
but mentioned it just in case.
I would like to do just that very much, but well, on newer Cygwins it doesn't work.
Then all modules are still there. And you're still in Cygwin.
To access modules which are in Windows version of Python from Cygwins version you add the Windows site-packages to the Python path variable.
On top of your program do:
import sys
sys.path.insert(0, "/cygdrive/c/Python27/lib/site-packages")
Then you can import them normally. But, this isn't exactly advisable.
For example, all modules that depend on windows paths could have great problems.
Or those written in C wouldn't exactly like been called from Cygwin.
Especially when Cygwin's Python version number and Windows's one aren't the same.
I tried with pyaudio, it collosaly crashed.
Some of them (mostly the little ones) will work just fine.
But, IMPORTANT NOTE here is, depending on the place you insert your Windows site-packages path the directory will be searched.
If you put it in place 0, Python will look at it firstly.
Then, it is perhaps better to use:
sys.path.append("/cygdrive/c/Python27/lib/site-packages")
If some package is packed into an EGG or ZIP archive, you will have to add it separately to the path.
The same goes for subdirectories. You try and keep thumbs up.
If you don't want to do that, you can:
import os
wd = os.getcwd()
os.chdir("/cygdrive/c/Python27/lib/site-packages")
# Your Windows imports here
os.chdir(wd)
Sometimes you will have to do both.
You can add even a check, so that your program works in both environments nicely:
if sys.platform=="cygwin": ...
It will work without a check, but it is stupid to bother Python with twice added same directory into the path.
Copying the packages will save you from these extra lines of code, but the problems I mentioned above will remain the same.
Never the less, if the modules are small, copy them, if not, do as I said, but be aware that it's not exactly something that is supposed to be done.

Related

using #!/usr/bin/env python3 shebang with Windows

I'm trying to run a Python script from the command line as a command on Windows -- so no usage of "Python" or ".py". If my script is named "testing.py", I am attempting to make this name into a command and call "testing" from the command line.
Going through the docs it seems I need to use this shebang #!/usr/bin/env python as long as I have Python in my PATH.
https://docs.python.org/3/using/windows.html#shebang-lines
I also have the script folder in my PATH, so something like
"testing.py" is currently working from the command line.
According to the docs and this tutorial,
https://dbader.org/blog/how-to-make-command-line-commands-with-python
I should be able to evoke my Python script just by "testing" if I have the proper paths within PATH and the above shebang. However, I can't seem to get the script running withouth adding ".py".
The accepted answer by #AKX is incorrect for Windows 10 standard Python 3, certainly in the latest Windows 10 (1903) if not earlier.
(Note: I cannot speak to how this may or may not work under WSL.)
I have several versions of Python installed (2.7, 3.6, 3.7, and most recently Python 3.8b1). I've been using the #!/usr/bin/env shebang for years in my scripts for cross-platform compatibility (usually to distinguish Py2 vs Py3 scripts).
I've created a little script in a folder (C:\so_test\awtest.py):
#!/usr/bin/env python3.6
import sys
print(sys.version)
If I run this with awtest.py or just awtest I get 3.6.x reported (showing it's running with Python 3.6). If I change the shebang to refer to 3.7, I get 3.7.x reported. If I change the shebang to just #!/usr/bin/env python3 I get the latest version of Python installed (3.8).
Now, if I add that folder to my path (path=%PATH%;C:\so_test in the command window you're testing in, or in the main env vars (you will need to restart the command window if you do the latter though)), I can change to a different directory and run awtest or awtest.py and they still work and refer to the folder in the path. If I remove the script folder from the path these files are no longer found.
While I wouldn't necessarily expect this to work on Windows prior to 10 or Python 2.7, this functionality appears to be the way of things going forward.
No, Windows does not support shebang lines.
The documentation you've linked relates to the py launcher installed by Python, which can interpret various shebang lines to choose a Python version to run a script with.
setuptools is able to generate wrapper .exes for your Python scripts, but it gets a little involved and already assumes you have a package with a setup.py and so on.
Locally, if you really, really need this, you probably could add .py to the PATHEXT environment variable, so the Windows command line looks up .pys like it looks up .exes (and various others; the current modern default is .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC). However, this will naturally not scale for distributing apps, as all of your users would need to set that too.
My recommendation is to stick with just that boring old python testing.py, really.
You can use shebang in windows by setting the path of your interpreter as the first line in the file(you will see a marker on VSCode that says 'set as interpreter ' on that line).
Using windows 10,Python version 3.9 see example:
#!C:/Users/waithira/AppData/Local/Programs/Python/Python39/python.exe
print('hello world')
if you're not going to do this often. You can still add a batch file to your path testing.bat containing the command that you need to execute your code.
#echo off
python testing.py
It's a borring workaround but it works without needing to mention the extention since windows interpret batch files the same way it interpret executables.

Python global environment screwed up

So, I've been toying around with python environments and I think i screwed mine up.
So when I ran python in shell, it would tell me i'm running on 2.7
I'm on windows 10, and i need a python switcher for my next project so i download pywin and used pywin to switch it to 3.5
the command i used was
pywin setdefault 3.5
now when i type python it says
python is not recognized as an internal or external command.
but py produces
Python 2.7.12
Now i can't use pip, easy_install, virtualenv
all of these commands i used to use, i suddenly no longer have access to them.
I tried switching back
but it wont even recognize pywin anymore.
The best way to check which Python version is executed is to check your environment variables. Another way of checking this is using the which command. (open cmd and run which python).
But, first you need to start a new cmd prompt to ensure your environment variables are not altered.
On Windows, but also any OS, you need to check the PATH and PYTHONPATH variables.
For Windows, follow the recommandations available in the Python documentation.
If you're not very experienced with working with windows, installations, and other similar things, I would recommend that you uninstall python, delete all versions/folders containing python (compiled) files (those that were installed with python, not the ones you've written) and then reinstall python. The installer should re-set the path variable to the correct location.

What is the difference between these 4 different types of Python shebangs on Windows?

I just came across shebangs (#!) for the first time while learning Python and I'm still trying to understand how exactly they work. After doing some research, I landed on this page where it listed four different types of shebangs. However, I'm not really sure what the difference is in Windows.
#! /usr/bin/env python
#! /usr/bin/python
#! /usr/local/bin/python
#! python
Now, I'm betting that the first one has something to do with virtual environments, but I'm not quite sure what. And according to this StackOverflow thread, the paths listed above actually are for POSIX systems, not for Windows... which confounds me even more because they are somehow being translated into Windows directories through some magic. Here's a quote:
A bin directory is created on POSIX systems only . . . Some paths
within the virtualenv are slightly different on Windows: scripts and
executables on Windows go in ENV\Scripts\ instead of ENV/bin/ and
libraries go in ENV\Lib\ rather than ENV/lib/.
Can anyone give a beginner a little more information on how exactly shebangs like these work on Windows?
The documentation is not totally explicit, but on my reading of it, there is no difference between those shebang lines. The shebang handling on Windows is purely "virtual" --- that is, the paths in the shebang lines are not actually mapped onto any paths on the Windows file system. Rather, the use of any of these "virtual paths" simply means "use the default system Python when running this file via the py Python launcher". The purpose of allowing the shebang line on Windows is to let the Python script specify arguments to Python, or Python versions (e.g., python3). You can find more info about how the default system Python is determined, how to override it, etc., in the documentation linked above.
Incidentally, note that on Windows these shebangs are only used when you run the Python script using the py launcher.

Ghost variable "idle" in terminal/cygwin?

I cannot figure out where, how, or why the variable "idle" has been set.
It is not in an alias
It isn't in my .bashrc file
It isn't in my PATH
It isn't a user-entered variable, because it doesn't show up when I execute this: ( set -o posix ; set ) | less
but, when I type cygstart idle somehow the program still starts. Why is this?
something that is also curious is that when I execute just idle, i.e. without the cygstart command, it attempts to execute a python file. Any ideas what might be going on?
Perhaps I am looking for suggestions about how to investigate, since I realize that there might be other problems specific to my machine.
Thanks.
Since 3.4, PSF CPython Windows installers put /Scripts in the pythonxy directory. Pip and easy-install are pre-installed in Scripts. When pip is used to install packages, associated executables are put in the corresponding /Scripts. If pythonxy is the default python installation, pythonxy/Scripts is added to the system PATH and idle.bat is added. (At least, this is what happened on my machine.) So yes, command prompt> idle should work as you describe, at least for 3.4.
It's not a variable. IDLE is the name of a Python IDE.
You can launch it either directly or through Cygwin.
cygstart can be given the name of a file or program. If you give it the name of a program, it will run it.

mingw + python eagerly translating path

I am using:
Windows XP
Python 2.6.2 (standard install from python.org)
git version 1.6.5.1-preview20091022 (installed from http://code.google.com/p/msysgit/)
I have an environment variable looking like an absolute path (/path/to/dir) but I'm using it to construct a git URL. At some point, it's getting translated to C:/Program Files/Git/path/to/dir. It seems like Python is at fault:
In a git bash shell:
$ export VAR=/path/to/dir
$ echo $VAR
/path/to/dir
$ python
>>> import os
>>> os.environ['VAR']
'C:/Program Files/Git/path/to/dir'
git bash is not translating the path, but Python is?
In a Windows Command Prompt, Python gets it right:
C:\>set VAR=/path/to/dir
C:\>echo %VAR%
/path/to/dir
C:\>python
>>> import os
>>> os.environ['VAR']
'/path/to/dir'
Can anyone explain what's going on here? And how can I prevent the translation in a bash shell?
EDIT: I should add that my python script runs on OS X and Windows, so if anyone does have a solution it would be good if worked on both platforms.
The problem definitely sounds like it's caused by MSYS. When an MSYS process execs a non-MSYS process (e.g. your msysgit bash shell calling native Windows Python), the arguments are checked for anything that looks like an absolute POSIX path (e.g. things that start with a single '/') and these are translated to the underlying 'real' Windows path so that the non-MSYS program can find them. It's likely that this same process happens to the contents of your environment variables too, for the same reason.
This would be why removing the leading '/' works (the value doesn't look like a POSIX path any more), why adding an extra one works (ditto), and why this works fine under Cygwin (it's not MSYS). I'm also guessing that you installed msysgit at 'C:\Program Files\Git', and that this is why MSYS thinks its 'fake' POSIX file hierarchy is rooted there and adds it to the front of '/path/to/dir' for you.
Unfortunately, if that is the explanation then there isn't a clear solution. I hit a similar issue trying to pass remote paths through ssh and haven't found any good ways round this myself, and according to the discussion at http://comments.gmane.org/gmane.comp.gnu.mingw.msys/4511 (from 2008) there simply isn't an obvious fix beyond the workarounds you've found so far. If this turns into a bigger problem for you, you might want to raise it on the MinGW-MSYS mailing lists or bug tracker. According to the Gmane discussion, it had never been reported formally despite being a known issue.
My guess would be that this is not python at fault, but the git bash shell.
Maybe the git bash shell is lying to you when you look at the variable.
Or, try to not put the first / and add it again later (if translation does not occurs).
If I try with cygwin, it works:
$ export test="/bin"
$ python
>>> import os
>>> os.environ["test"]
'/bin'
The console you get from msysgit is probably modified for git user's needs, from my POV, it is only useful for simple tasks and to access git command line not to develop and run python scripts (you are using a Python installation for Windows in a shell installed for a specific application, that doesn't sound good).
You should install Cygwin and his python package (and even git package if you want) to get a correct POSIX environment with binaries and libraries prepared for it.

Categories

Resources