Reputation: 405
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?
Upvotes: 2
Views: 1162
Reputation: 4236
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.
Upvotes: 2