So I've been following this django tutorial, and I've been running into some issues with the manage.py commands when using a git bash terminal. I'm using Python 2.7.11 (Anaconda 2.4.1), Django 1.9.5 and git 2.8.1.windows.1 on Windows 10.
Specifically the python manage.py runserver and the python manage.py shell commands don't appear to work properly using a git bash terminal, but they do work when using an Anaconda terminal. I'd like to find a way to get it working with git bash.
According to the tutorial, I should see this output on terminal when running python manage.py runserver:
Performing system checks...
System check identified no issues (0 silenced).
April 19, 2016 - 15:50:53
Django version 1.9, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Using git bash, I know that SOMETHING is working because I can navigate to http://127.0.0.1:8000/ in my browser. I just don't see any output on the terminal.
I continued along with the tutorial, because this didn't really affect anything. It appears to work, just no terminal output. Then I got to this part of the tutorial, where you open a shell with python manage.py shell.
Using git bash, nothing happens. The shell prompt never opens, as if it's stuck. No errors, just a blinking cursor on a new line without a command prompt. If I do the same thing using the Anaconda terminal, the shell opens fine.
At first I thought it was an installation error. So, I attempted to install django using both pip install and conda install, but I still get the same problems. I'm not really sure what's going on here, and I haven't managed to find a solution yet.
Any help is greatly appreciated.
Update:
I found a similar question that doesn't quite have a solution, but it recommends using pystuck to debug. I read the pystuck docs but don't quite understand how to use it for this problem in particular.
I have this problem too, when running python manage.py runserver the verbose output is missing, only high level page responses are present. I was using powershell or Git Cmd, that is an option... This fix also resolves issues with using Git Bash to python manage.py makemigrations and python manage.py migrate,
Option: Use Git Cmd or powershell.
Temp Fix: Run this command in Git Bash to generate verbose output for this working session: alias python='winpty python.exe'
Perm Fix: Use the temp fix in a .bashrc file, git bash will always have verbose output.
cd ~ #Takes you to your root directory.
touch .bashrc
Edit the file using VIM, or editor of your choice, add the following line (note: Notepad from MS Windows will not save this file correctly. Do not use notepad.):
alias python='winpty python.exe'
Restart Git Bash.
Note:
In some circumstances, you will get one prompt when you launch git bash:
"WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or
~/.profile.
This looks like an incorrect setup. A ~/.bash_profile that
loads ~/.bashrc will be created for you."
You will notice a second file .bash_profile was created for you and you are good to go!
That's it... enjoy.
I don't know why but I found a bit of a workaround. When running python manage.py runserver in the Git Bash window on windows 7 (using Anaconda2) I also DID NOT get the expected:
Performing system checks...
System check identified no issues (0 silenced).
February 09, 2017 - 17:39:48
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8090/
Quit the server with CTRL-BREAK.
Doing the same in the native windows CMD prompt worked!!!
I had manage.py open in Sublime Text and when I saved it (or any other file in the project) the server (started and currently frozen in the git bash window) started up as expected.
Maybe this will work for you. Don't know why it does or should...
Related
Description
I have been using VSCode for a while now. Currently, the Code Runner extension enables me to run Python scripts, using custom commands. However, I would like to know if VSCode is able to perform additional actions, compared to the Code Runner extension, which just runs my custom command pipenv run python -u {open_file_path}.
Goal
I currently use pipenv for my Python projects. I would like VSCode to launch a pipenv shell and execute a couple of commands to run my Python scripts.
Here are the steps I hope VSCode is able to automate when pressing F5 to start debugging.
Launch a pipenv shell if one is not already launched.
pipenv shell
Change directory to code source directory.
cd {project_root/src}
Run the script inside the pipenv shell.
python {open_file_path}
I have read that the launch.json is able to run preLaunchTask. It seems that it could resolve point one of launching pipenv shell. I am still trying to figure out how this works.
Any help or pointers is greatly appreciated.
Platform / Version
Platform: Windows 10
Python Version: 3.9.9
Pipenv Version: 2021.5.29
VSCode Version: 1.62.3
VSCode Microsft Python Extensions Enabled: Yes
I have recently started a new project in PyCharm, finally utilizing anaconda environments. However, after trying to make my first commit through PyCharm, it appears to be using my native python, not the environment set in PyCharm. I've tried to restart PyCharm several times, restarted my computer, and reinstalled the virtual environment.
Here is a copy of pre-commit hook:
set -e
# Run linters and tests
source scripts/lint.sh
The linter is the following: (which python has been added to highlight the issue)
set -e
set -v
which python
flake8 ~project name~
mypy ~project name~
pytest -x
black --check --fast --quiet ~project name~
set +v
I am running the commit through PyCharm -> VCS -> Commit. Inside PyCharm, the commit fails
(below this are a large amount of mypy errors, but note the environment)
However, if I run the commit from the terminal with $ git commit -m "testing commit" the commit works. It provides the following response:
This is the correct virtual environment inside of the project, seen here:
Am I setting something up incorrectly? I vastly prefer PyCharm's VCS and would prefer not to have to use git from the terminal.
PyCharm doesn't run git hooks under the virtual environment. The relevant ticket in the bug tracker: https://youtrack.jetbrains.com/issue/PY-12988
Here's what worked for me.
I was getting the following error:
18:37 Commit failed with error
0 file committed, 1 file failed to commit: Update pre-commit hooks
env: python3.7: No such file or directory
When I navigated to .git/hooks/pre-commit in my project repo, it turned out that the shebang line is #!/usr/bin/env python3.7.
This was an issue, since calling python3.7 on my MacOS would end up with the following:
zsh: command not found: python3.7
I could have either added a global python3.7 or, alternatively, updated the shebang. I went with the latter one and changed the shebang line to:
#!/usr/bin/env python3
This resolved the issue for me.
It seems that the aforementioned PyCharm ticket won't be fixed soon (it's there since 2014).
This hack below works for me; I added this to the PyCharm ticket:
This is a slightly annoying workaround that works for me:
Close PyCharm.
cd /your/project/dir
Open PyCharm from the command line: PYENV_VERSION="$(pyenv local | head -1)" open /Applications/PyCharm.app/. I'm using macOS, you
should adapt the open command to your OS.
I have to do it every time I switch projects, otherwise the pylint
pre-commit hook doesn't work. If you have a
similar config for your projects (Python version and not using
PyLint), just run PyCharm from the CLI once.
You can manually edit the auto-generated pre-commit file (located in your project dir at .git/hooks/pre-commit) to add the path to your virtual environment, replacing:
# start templated
INSTALL_PYTHON = 'PATH/TO/YOUR/ENV/EXECUTABLE'
with
# start templated
INSTALL_PYTHON = 'PATH/TO/YOUR/ENV/EXECUTABLE'
os.environ['PATH'] = f'{os.path.dirname(INSTALL_PYTHON)}{os.pathsep}{os.environ["PATH"]}'
None of the above solutions worked for me: PyCharm 2020.3 on Windows 10
What I did is to rename the .git\hooks\pre-commit -> .git\hooks\pre-commit.py
and created a new .git\hooks\pre-commit with next content:
#!/bin/bash
BASEDIR=$(dirname "$0")
/c/<PATH-to-YOUR-Python>/python.exe $BASEDIR/pre-commit.py $#
Worked as a charm!
I am new to stackoverflow, very new to Python and trying to learn Django.
I am on Windows 10 and running commands from powershell (as administrator).
I am in a virtual environment. I am trying to set up Django.
I have run the following commands
"pip install Django"
"django-admin.py startproject learning_log ."
"python manage.py migrate"
All of the above seemed to work okay, however, when I then try to run the command
"python manage.py runserver"
I get a popup error box that says:
Python has stopped working
A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is available.
Can someone tell me how to resolve this issue or where to look for any error messages that might clue me in as to what is causing the problem?
I finally got the "python manage.py runserver" command to work. The only thing different I did was before setting up the virtual env and installing Django was set my executionpolicy to Unrestricted. Previously it had been set to RemoteSigned. I hadn't been gettiing any warning or errors but thought I would try it and it worked.
I encountered the same problem. After trying everything, I switched from PS to cmd, cd to the same directory and run python manage.py runserver. Then it worked. Then I ctrl+C quit the server, switched back to PS, ran the command, it still threw the same dialog window (Python stopped working). Then I went back to cmd, typed the command and the server started fine.
Conclusion: Use cmd to run the command, not PS.
I'm new to django and currently going through the main tutorial. Even though it was working earlier, when I do python manage.py runserver OR python manage.py -h OR with any other command, the shell doesn't output anything. Wondering what I'm doing wrong.
The problem is that the first line in manage.py breaks the file on windows.
The first line should look like this:
#!/usr/bin/env python
Removing it will fix the issue.
First, check if python is fully installed by typing "python" in a shell.
Then you should try python manage.py runserver inside your django project. If you don't have any django project, try creating one by typing django-admin.py startproject mysite. If nothing is displayed in your shell, you must have installed Django the wrong way.
Please refer to Django Documentation at https://docs.djangoproject.com/en/1.4/intro/install/
If you had your server running till one point and a certain action/change broke it, try going back to the previous state.
In my case there was an email trigger which would put the system in an invalid state if email doesn't go through. Doing git stash followed by selectively popping the stash and trying the runserver helps narrow down the problem to a particular file in your project.
Please try this.
Uninstall Python.
Go inside C drive and search Django. You will get many Django related files.
Delete every Django file. đ don't delete your Django files.
Install Python.
It's worked for me.
if you created a virtual environment then activate it. you can try this command(in virtual environment directory) if you're using windows os:
.\Scripts\activate
On Ubuntu works for my by running manage.py as script:
./manage.py runserver
Just stuck with the same problem. Found a solution that works, but tedious.
You need to know the location of the python.exe file in your computer. It is usually
C:/Users/USERNAME/AppData/Local/Programs/Python//python.exe
Modify as required and run the following in CMD,
C:/Users/USER1/AppData/Local/Programs/Python/Python38-32/python.exe
F:/mysite/manage.py runserver
Hope this works :)
if you are using Redis Server on Windows, check it out if Redis Server is running, I had same problem and realized my Redis Server was not running, I ran it and now my manage.py commands work fine.
The same happened with me also, but this issue is a minor one as it happens if you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.
SO you should start from the beginning, uninstall Django first then,
create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
for e.g:
python3 -m venv tutorial-env
//This will create the tutorial-env directory if it doesnât exist, and also create directories inside it
Once youâve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
Now,
In the command prompt, ensure your virtual environment is active, and execute the following command:
...> py -m pip install Django
NOTE:
If django-admin only displays the help text no matter what arguments it is given, there is probably a problem with the file association in Windows. Check if there is more than one environment variable set for running Python scripts in PATH. This usually occurs when there is more than one Python version installed.
Another solution, if you can, is to upgrade Django
pip install django --upgrade
Oftentimes one will get other unrelated issues to solve that are linked with the upgrade but once all is fixed the server should run just fine.
If you can't upgrade Django, this problem also happens when the code was built using Python 2.x and you're locally using Python 3.x.
The quicker fix in that case is to uninstall Python 3.x from your machine and make sure Python 2.x was added to the path. I've seen some developers setting up alias in PowerShell to have more than one version in the environment too.
I think the problem is in manage.py file (50%), check it with an another file that is correct.
Python will not run in git bash (Windows). When I type python in the command line, it takes me to a blank line without saying that it has entered python 2.7.10 like its does in Powershell. It doesn't give me an error message, but python just doesn't run.
I have already made sure the environmental variables in PATH included c:\python27. What else can I check?
A session wherein this issue occurs looks like the following:
user#hostname MINGW64 ~
$ type python
python is /c/Python27/python
user#hostname MINGW64 ~
$ python
...sitting there without returning to the prompt.
Temporary solution
Just enter this in your git shell on windows - > alias python='winpty python.exe', that is all and you are going to have alias to the python executable. This alias will be valid for the duration of the shell session.
winpty is a Windows software package providing an interface similar to a Unix pty-master for communicating with Windows console programs.
Permanent solution
Add the command to your .bashrc in the users home directory. You can use the CLI or a text editor:
Using CLI
This can be accomplished from git bash like so:
echo "alias python='winpty python.exe'" >> ~/.bashrc
which will create .bashrc in the current users home directory if the file doesn't exist or append the alias to the end of .bashrc if it does.
Using a text editor
Alternatively, you could first create a .bashrc. Depending on your file manager, this may be easier to accomplish in git bash like so:
cd ~
touch .bashrc
At which point you can open .bashrc in your prefered text editor and add it there.
To apply the change, either use the command source .bashrc or restart the shell.
Update
Newer versions of Git no longer use .bashrc but instead use .bash_profile. Conda also uses this profile when initializing, so be sure not to overwrite or delete the initialization block. See more here: Git for Windows doesn't execute my .bashrc file.
I don't see next option in a list of answers, but I can get interactive prompt with "-i" key:
$ python -i
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55)
Type "help", "copyright", "credits" or "license" for more information.
>>>
This is a known bug in MSys2, which provides the terminal used by Git Bash. You can work around it by running a Python build without ncurses support, or by using WinPTY, used as follows:
To run a Windows console program in mintty or Cygwin sshd, prepend console.exe to the command-line:
$ build/console.exe c:/Python27/python.exe
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 10 + 20
30
>>> exit()
The prebuilt binaries for msys are likely to work with Git Bash. (Do check whether there's a newer version if significant time has passed since this answer was posted!).
As of Git for Windows 2.7.1, also try using winpty c:Python27/python.exe; WinPTY may be included out-of-the-box.
I am windows 10 user and I have installed GIT in my system by just accepting the defaults.
After reading the above answers, I got 2 solutions for my own and these 2 solutions perfectly works on GIT bash and facilitates me to execute Python statements on GIT bash.
I am attaching 3 images of my GIT bash terminal. 1st with problem and the latter 2 as solutions.
PROBLEM - Cursor is just waiting after hitting python command
SOLUTION 1
Execute winpty <path-to-python-installation-dir>/python.exe on GIT bash terminal.
Note: Do not use C:\Users\Admin like path style in GIT bash, instead use /C/Users/Admin.
In my case, I executed winpty /C/Users/SJV/Anaconda2/python.exe command on GIT bash
Or if you do not know your username then execute winpty /C/Users/$USERNAME/Anaconda2/python.exe
SOLUTION 2
Just type python -i and that is it.
Thanks.
Try python -i instead of python, it's a cursor thing.
Git Bash Workaround- Launch Python 2 & Python 3 with aliases
HI. This is (for me) the best solution to run both Python (Python 2.7 and Python 3.x) directly from Git Bash on Win 10 => adding aliases into the aliases file that Git Bash uses for.
Git Bash aliases file is aliases.sh. It is located in:
C:\path where you installed Git\etc\profile.d\aliases.sh
1) Open (with a text editor like Atom or other) the aliases.sh
for ex: in my case the file is in C:\Software\Develop\Git\etc\profile.d\aliases.sh
2) Add your alias for Python
In my case the python.exe are installed in:
C:\Networking\Network Automation\Python 2.7\python.exe
C:\Networking\Network Automation\Python 3.7\python.exe
So you must create 2 aliases, one for Python 2 (I named python2) and the other for Python 3 (I named just python)
Git Bash uses linux file structure so you need to change the "\" for "/"
and if you have a path like my example Network Automation you put it with " "
"Network Automation", for ex.
winpty is the magic command that will call the executable.
So add these lines at the beginning of aliases.sh
alias python2='winpty C/Networking/"Network Automation"/"Python 2.7"/python.exe'
alias python='winpty C/Networking/"Network Automation"/"Python 3.7"/python.exe'
3) Add or Modify other aliases (if you want)
I modified also the ll alias to show all the files and in a human readable list:
alias ll='ls -lah'
4) Save the aliases.sh file
5) OK!!! close and relaunch your Git Bash
Now, permanently you could launch both Python directly from Git shell just writting
$ python -> launch Python 3
$ python2 -> launch Python 2
$ ll -> enters a ls -lah to quickly show your detailed file list
Cheers, Harry
2 workarounds, rather than a solution: In my Git Bash, following command hangs and I don't get the prompt back:
% python
So I just use:
% winpty python
As some people have noted above, you can also use:
% python -i
2020-07-14: Git 2.27.0 has added optional experimental support for pseudo consoles, which allow running Python from the command line:
See attached session.
In addition to the answer of #Charles-Duffy, you can use winpty directly without installing/downloading anything extra. Just run winpty c:/Python27/python.exe. The utility winpty.exe can be found at Git\usr\bin. I'm using Git for Windows v2.7.1
The prebuilt binaries from #Charles-Duffy is version 0.1.1(according to the file name), while the included one is 0.2.2
type: 'winpty python' and it will work
gitbash has some issues when running any command that starts with python. this goes for any python manage.py commands as well. Always start with 'winpty python manage.py' At least this is what works for me. Running Windows 10.
You can change target for Git Bash shortcut from:
"C:\Program Files\Git\git-bash.exe" --cd-to-home
to
"C:\Program Files\Git\git-cmd.exe" --no-cd --command=usr/bin/bash.exe -l -i
This is the way ConEmu used to start git bash (version 16). Recent version starts it normally and it's how I got there...
In addition to #Vitaliy Terziev answer
try touch .bash_profile and then add alias into the file.
I am using MINGW64 via Visual Studio Code on Windows 10 and trying to install node-sass (which requires python2). I followed felixrieseberg/windows-build-tools #56 on Github which solved my issue.
This is a special case, but I'm posting in case someone has the same problem:
npm --add-python-to-path='true' --debug install --global windows-build-tools
This installs python and other required build tools to %USERPROFILE%\.windows-build-tools\python27.
For python version 3.7.3 in vscode with gitbash as the default terminal I was dealing with this for a while and then followed #Vitaliy Terziev advice of adding the alias to .bashrc but with the following specification:
alias python=ââ/c/Users/my user name/AppData/Local/Programs/Python/Python37/python.exeââ
Notice the combination of single and double quotes because of âmy user nameâ spaces.
For me, "winpty" couldn't resolve python path in vscode.
Type the command PY instead of Python. Invoking the Interpreter
(python.org).
I know this is an old post, but I just came across this problem on Windows 10 running Python 3.8.5 and Git 2.28.0.windows.1
Somehow I had several different 2.7x versions of Python installed as well. I removed every version of Python (3x and 2x), downloaded the official installer here, installed 3.8.5 fresh (just used the defaults) which installed Python 3.8.5 at this location:
C:\Users\(my username)\AppData\Local\Programs\Python\Python38
Then to get the command python to work in my git bash shell, I had to manually add the path to Python38 to my path variable following the instructions listed here. This is important to note because on the python installer at the bottom of the first modal that comes up it asks if you want to add the python path to your PATH environment variable. I clicked the checkbox next to this but it didn't actually add the path, hence the need to manually add the path to my PATH environment variable.
Now using my gitbash shell I can browse to a directory with a python script in it and just type python theScriptName.py and it runs no problem.
I wanted to post this because this is all I had to do to get my gitbash shell to allow me to run python scripts. I think there might have been some updates so I didn't need to do any of the other solutions listed here. At any rate, this is another thing to try if you are having issues running python scripts in your gitbash shell on a Windows 10 machine.
Enjoy.
Another example of this issue is using the AWS Elastic Beanstalk command line interface (awsebcli, eb cli) from the git bash (MINGW64, Mintty) in windows (using git version 2.19.0.windows.1).
I'm just posting this because it took me a while to end up here, searching for eb-cli specific issues.
Commands such as eb init or eb config save, which require user input, appear to cause a freeze/hang. In reality I guess the console is not updated with the text requesting user input. Moreover, eb deploy only updates the console text after the command has finished, so I don't get to see progress updates until finished.
As mentioned in the git for windows release notes (for v2.19.0) and e.g. in Xun Yang's answer, a workaround is to run
winpty eb <command> (instead of just eb <command>)
A alternative, as suggested in this git for windows issue, could be to use the windows native console instead of mintty (option during git installation).
The one worked for me is as mentioned earlier in these great answers above is the alias as follows:
(I'm using anaconda, so first find where is the python path, then add it into the alias on git bash).
1. on anaconda terminal I run: where python
2. on git bash I run: alias python='winpty "C:\ProgramData\Anaconda3\envs\your_env_name\python.exe"'
3. Done. Python is defined inside the git Bash using the alias.
Thanks to (Vitaliy Terziev & hygull) for their very helpful answers.
python.exe -i works but got issues in exiting from the interactive mode by sending "^Z" (CTRL+Z). So, seem better to use winpty python.exe in Git Bash for Windows.
Use ~/bin directory to make a wrap/reference file (like ~/bin/python) which will be accessible everywhere (you may use different version reference like ~/bin/python37).
Code inside the file:
#!/usr/bin/env bash
# maybe declare env vars here like
# export PYTHONHOME=/c/Users/%USERNAME%/.python/Python36
# export PATH="${PATH}:/c/Users/%USERNAME%/.python/Python36"
# replace %USERNAME%,
# or use "~" instead of "/c/Users/%USERNAME%" if it works
winpty /c/Users/%USERNAME%/.python/Python36/python.exe ${#}
I just don't like these "magic" aliases which you're always forgetting where it's coming from, and sometimes leads to issues in some cases.
Use ~/bin/python file and -i parameter:
#!/usr/bin/env bash
if [ -z "${#}" ]; then
# empty args, use interactive mode
/c/Users/%USERNAME%/.python/Python36/python.exe -i
else
/c/Users/%USERNAME%/.python/Python36/python.exe ${#}
fi
if you run a Windows PowerShell command and an error occurs, the error record will be appended to the âautomatic variableâ named $error.
You can use the $error variable to find the errors, in the same PowerShell session.
The $Error variable holds a collection of information, and thatâs why using $Error[0] can get to your error message objects. Also the $Error[0] variable will hold the last error message encountered until the PowerShell session ends.
Have a look at this answer:
Git Bash won't run my python files?
the path in Git Bash should be set like this:
PATH=$PATH:/c/Python27/