This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: venv activate bash script has wrong line endings in Windows
Type: behavior Stage:
Components: Windows Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Kevin, astanton1978, erik.bray, eryksun, paul.moore, r.david.murray, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2017-12-30 04:13 by Kevin, last changed 2022-04-11 14:58 by admin.

Messages (8)
msg309215 - (view) Author: Kevin (Kevin) Date: 2017-12-30 04:13
attempts from within cygwin:

1. The Posix way:

$ python -m venv taco
$ cd taco
$ source bin/activate
-bash: $'\r': command not found
-bash: Scripts/activate: line 4: syntax error near unexpected token `$'{\r''
'bash: Scripts/activate: line 4: `deactivate () {

2. The windows way:

$ python -m venv taco
$ cd taco
$ /full/path/to/venv/taco/scripts/activate.bat
$


3. the only solution from cygwin (still not 100% functional):

$ python -m venv taco
$ cd taco
$ cd Scripts
$ cmd
C:\taco\Scripts\> activate.bat

(taco) C:\taco\Scripts\>




HOWEVER. when running "pip freeze" in number 3 it returns the system packages despite the "include-system-site-packages = False" in pyvenv.cfg 
When #3 is run inside command prompt "pip freeze" returns nothing correctly.


Come on guys, please don't make me use command prompt or powershell.
msg309258 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2017-12-30 21:07
cygwin in not currently fully supported (there are people working on it, though).  Can you determine if this is a bug in cygwin's bash support, or something else?  It certainly works with bash shell on unix.  I would presume that from cygwin one would want the posix way to work, and that it should be possible.
msg339985 - (view) Author: Andrew Stanton (astanton1978) Date: 2019-04-11 16:26
I dont think this is a Cygwin only problem, as this is happening to me when running WSL (Windows subsystem for Linux). I can run Scripts\Activate.ps1 from powershell, Scripts\activate.bat from cmd, but running scripts/activate from wsl reports this same error.
msg340038 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-04-12 13:16
Why is the bash/zsh "activate" script getting distributed with CRLF line endings? Apparently Windows builds of bash (e.g. git-bash) can handle CRLF line endings, but Cygwin bash cannot.

As to Linux bash in WSL, the activate script from a virtual environment created by Windows Python can't be used anyway. It modifies $PATH using a DOS path like "C:\taco" as opposed to a WSL path like "/mnt/c/taco".

> when running "pip freeze" in number 3 it returns the system 
> packages despite the "include-system-site-packages = False" 
> in pyvenv.cfg 

The 3rd attempt is just running activate.bat from a CMD shell. That should work, unless you `exit` out of CMD back to bash. Anyway, check `where.exe pip` after activating. It should find "C:\taco\Scripts\pip.exe" first.
msg340052 - (view) Author: Steve Dower (steve.dower) * (Python committer) Date: 2019-04-12 14:55
It probably just needs a gitattributes fix so it doesn't get converted when cloned to make the release build.

This is assuming that in all these cases people are installing Python for Windows and trying to use it from a non-Windows shell (both WSL and Cygwin have their own "native" Python builds that presumably work). I can't tell if there's something else going on here.
msg340065 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-04-12 15:35
> This is assuming that in all these cases people are installing Python 
> for Windows and trying to use it from a non-Windows shell (both WSL 
> and Cygwin have their own "native" Python builds that presumably 
> work).

It should work from Cygwin and MSYS2 since they support DOS paths in $PATH. I don't have Cygwin installed, but I tried MSYS2 bash and Git bash, and it worked fine, even with CRLF line endings. Apparently only Cygwin bash has a problem with CRLF line endings. 

The issue with using Windows Python in this case is that the terminals for these environments emulate Unix PTYs with named pipes (e.g. "msys-0123456789abcdef-pty0-from-master" for stdin and "msys-0123456789abcdef-pty0-to-master" for stdout and stderr). Thus isatty() is false, and interactive mode has to be forced with the `-i` command-line option.
msg340272 - (view) Author: Erik Bray (erik.bray) * (Python triager) Date: 2019-04-15 14:06
Coincidentally I just encountered this myself when trying to activate a venv created by Python for Windows while in a Cygwin shell.  Indeed, bash for Cygwin is just like bash anywhere else, and considers it a syntax error to encounter unexpected CR's.

Alas, venv creates <path>/<to/<env>/Scripts/activate with CRLFs.  After running dos2unix on it, this script can be sourced from Cygwin and otherwise works fine.

The issue that arises with interactive mode is a known issue not specific to Python or venv.  It can also be worked around by running python with winpty.
msg388962 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-03-17 19:57
The POSIX "Lib/venv/scripts/common/activate" script needs a line-ending exception in ".gitattributes":

https://github.com/python/cpython/blob/master/.gitattributes
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76632
2021-03-17 19:58:00eryksunlinkissue43437 superseder
2021-03-17 19:57:39eryksunsettitle: python -m venv activation issue when using cygwin on windows -> venv activate bash script has wrong line endings in Windows
messages: + msg388962
versions: + Python 3.9, Python 3.10, - Python 3.6, Python 3.7
2019-04-15 14:06:37erik.braysetmessages: + msg340272
2019-04-12 15:35:08eryksunsetmessages: + msg340065
2019-04-12 14:55:14steve.dowersetmessages: + msg340052
2019-04-12 13:16:47eryksunsetversions: + Python 3.7, Python 3.8
nosy: + paul.moore, tim.golden, eryksun, zach.ware, steve.dower

messages: + msg340038

components: + Windows
2019-04-11 16:26:05astanton1978setnosy: + astanton1978
messages: + msg339985
2017-12-30 21:07:50r.david.murraysetnosy: + r.david.murray, erik.bray
messages: + msg309258
2017-12-30 04:13:49Kevincreate