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!
Related
I am trying to run some pytests on vscode in a conda environment. The connection to my postgres db is handled by flask/flask-sqlalchemy and I have installed the add-ons for pytest (pytest-flask, pytest-postgresql), to use some fixtures.
When I'm now trying to run a test (or all of them), I get the following error message:
FileNotFoundError: [Errno 2] No such file or directory: '/home/robert/software/anaconda3/envs/python38/bin/pg_ctl'
Now, this is somewhat obvious because the conda env indeed does not have a pg_ctl module. It is rather the (system-wide) installation of postgres, that has it. I tried to modify PATH, including the path to the pg_ctl module via the .profile config file, but:
!! once I execute the run command in vscode, some process either triggered by the vscode testing extension or conda (or both?!?!) modifies the PATH envvar so that the error occurs.
When I run pytest .in the terminal, the test runs fine.
When I run "Debug Test" the test runs fine.
Anybody any clue??
Setup: vscode 1.65, conda 4.9.2, python 3.8
I'll try to provide an MVE once I find out how to reproduce it...
Could you add this to your settings.json file?
"terminal.integrated.env.osx": {
"PYTHONPATH": "{the path of pg_ctl folder}"
},
When terminal settings are used, PYTHONPATH can affect any tools that are run within the terminal.
You can refer to the official docs for more details.
I'm running ansible-playbook via something like
subprocess.run(['bash', '-c', "ansible-playbook some_playbook.yml"])
After uninstalling & reinstalling ansible-playbook my project somehow can't execute it anymore claims "bash: ansible-playbook: command not found".
Facts:
running on Ubuntu 20.04
ansible-playbook was installed via pip3
Tried the following:
ensured that the command is available and runnable via terminal & python3
deleted the venv folder and reinstalled everything (using PyCharm does it automatically)
deleted Bash cache via hash -r & hash -d ansible-playbook
rebooted
installed pyclean and ran it
deleted .idea folder (PyCharm)
recloned the repository into different directory and set up everything again. Same problem.
created a dummy test in the same project folder and found out that it runs fine:
> cat test.py
import subprocess
process = subprocess.run(['bash', '-c', "ansible-playbook some_playbook.yml"])
Everything worked fine before the reinstall. My program also runs various Bash commands before executing ansible-playbook and I swear on my left testicle that it's not my code. There must be some hidden mystery cache I'm missing.
Thanks for any suggestion!
To make my comment an answer:
If you've installed the package as a regular user with a recent pip, it will have installed the libraries and binaries into ~/.local/bin which isn't on PATH by default. (Pip will warn you about that.)
Once the binary is on PATH, you can skip the extra shell process (which is unnecessary and may cause Weird Things in certain situations) and not be vulnerable to shell injection attacks with
import subprocess, shutil
subprocess.run([shutil.which("ansible-playbook"), "some_playbook.yml"])
When I switch my PyCharm to use Python 3.4.3 and I am getting the error:
Invalid Python SDK
Also PyCharm does not automatically find the Python 3.4 interpreter for me, even though it is on the desired path /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4. See the screenshot:
Despite this the interpreter does work. I am able to get correct output as expected, but code completion related to Python 3.4 is not working.
E.g. print("hello world!) still shows an error on the editor, but the console shows the correct output.
This also happened to me. I renamed a repository and then my virtual environment got stuck in the old path.
I grepped all configuration files and could not find any reference to the old one.
What finally solved the problem was to clear caches with the option File > Invalidate Caches / Restart...:
I had the same issue.
Try to comment/remove the PYTHONPATH variable in your ~/.bash_profile
#export PYTHONPATH=/usr/local/lib/python2.7/site-packages/
If it does not help it also may be useful to look in the idea.log for the errors:
/Users/username/Library/Logs/PyCharm40/idea.log
I had the following errors:
Your PYTHONPATH points to a site-packages dir for Python 2.x but you are running Python 3.x!
PYTHONPATH is currently: "/usr/local/lib/python2.7/site-packages/"
You should `unset PYTHONPATH` to fix this.
I go the same error message in Windows version of PyCharm after I re-installed Python (3.5) on a different location. The problem was that most of my existing virtual environments registered in PyCharm were still referencing the old installation of Python.
I resolved it by deleting these existing virtual environments and creating a new one. If deleting existing interpreters (in virtual environment) is not an option, you should be able to modify them instead in File | Settings | Project Interpreter
I got the same issue, when I updated Python (3.x) version via Home brew in MacOS.
Above answers didn't work for me. But with those, I realize that, it's an issue with linking the directories. I deleted ~/.virtualenvs folder and recreated all virtual envs.
$ ls -a ~/.virtualenvs
local-dev wdias-dev
$ rm -rf ~/.virtualenvs
$ which python3
/usr/local/bin/python3
$ mkvirtualenv local-dev --python=/usr/local/bin/python3
$ mkvirtualenv wdias-dev --python=/usr/local/bin/python3
Open the PyCharm again, and it works fine.
I couldn't get anything to work, so I cloned my conda env (see how), called it something else, and then set it in PyCharm to the new one.
For me, changing the paths in ~\venv\pyvenv.cfg made PyCharm recognize the new environment after bringing my projects to a new PC.
I had the same issue in Windows10. I was so frustrated, beacause everything seems looked OK. I've added PYTHONPATH, I've restarted Pycharm and deleted old virtualenv folders and created new... It did't work.
And at the end I have just opened Pycharm in an administrator mode and it works!
When creating a new project my interpreter was set to python 2.7 so I had to change that to python3 and everything works like a charm
I got the same problem with Pop OS 21.04 and Pycharm installed via Flatpack. So i remove it and installed via Snap and started working again.
I ended up having to install Python as an admin and make sure it installed to the C:\Program Files\Python310 folder. I think The option was to make sure it installed for all users. If I installed it under for user's appdata it wouldn't work.
Once I did that I was able to point pycharm to the C:\Program Files\Python310\ for the interpreter. Pretty annoying but finally got it working
I've solved it too. Uninstalled python 3.10, installed python 3.9. Changed the python interpreter to 3.9 through pycharm setting. Created a new virtual environment.
I installed git-filter-repo via scoop, tried multiple git filter-repo commands e.g. git filter-repo -h, they all logged nothing, no warning or error, just nothing.
Tried rebooting, reinstalling, and installing it on another Windows 10 computer, all reproduced it.
git-filter-repo: v2.33.0
git: v2.33.0.windows.2
python: v3.9.7
scoop:
Current Scoop version:
09200504 (HEAD -> master, origin/master, origin/HEAD) reset: skip when app instance is running (#4359)
'main' bucket:
b71f4a842 (HEAD -> master, origin/master, origin/HEAD) nunit-extension-vs-project-loader: Update to version 3.9.0
How to solve this issue?
(Now updated for newer Python installers.)
When I installed git-filter-repo on Windows earlier this year, the following steps worked for me:
Download and install Python for Windows. In newer installers you need to go into the Advanced Options to make sure Python is added to your Path:
Confirm python was added to your path and that you can run either the command python --version or python3 --version from your Git command line. (I recommend Git Bash.) In my case, my executable name is python and if yours is too, you will need this in step #7 below.
Clone git-filter-repo from GitHub.
git clone https://github.com/newren/git-filter-repo.git
Run the command git --exec-path to see your Git exe directory.
From the git-filter-repo repo's root directory, copy the file git-filter-repo (about 160KB) into your Git exe directory.
In your command line where you use Git, type the command git filter-repo. If it works, you should get the message "No arguments specified." and you can skip step #7. If it doesn't work, it's likely that your python exe is python instead of python3 as determined in step #2. Go to the next step.
If you get no message or an error message similar to "/usr/bin/env: ‘python3’: No such file or directory", then edit the file git-filter-repo that you copied into your Git exe directory in step #5, and change the first line from "python3" to "python".
Now be amazed at how fast and awesome git-filter-repo is.
Still having problems? If you didn't add the environment variable in step #1, some people have had luck in step #7 by changing their python command to just "py". This is the python launcher which can auto-detect the highest version installed on your machine. More info here. I should point out that this did not work for me with python 3.10.7. I actually tried this first but ended up re-installing and enabling the option to "Add python to environment variables" as described above in step #1.
In my case I followed this answer for windows 11 and here is my experience.
I installed python from the windows store or from this link https://www.python.org/downloads
I run pip3 install git-filter-repo or python3 -m pip install --user git-filter-repo only for current user.
I got this message:
Requirement already satisfied: git-filter-repo in c:\users\username\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages
Now copy that folder but replace site-packages with scripts.
Path: c:\users\username\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\scripts
You will find git-filter-repo.exe inside the scripts folder.
Run git --exec-path
You will get
C:/Program Files/Git/mingw64/libexec/git-core
Copy git-filter-repo.exe to a folder in step 4 (C:/Program Files/Git/mingw64/libexec/git-core).
Now you should be able to run git filter-repo
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...