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.
Related
I am using Liquibase (http://www.liquibase.org) to try and diff two databases. Liquibase is installed and running fine from CLI, however when I try to fun the same command from Python 3.7 using subprocess and shlex i can FileNotFoundError: [Errno 2] No such file or directory: 'liquibase': 'liquibase'
When I add subprocess.call('pwd') to Python script, I get the same directory as when I am executing the command from terminal. Liquibase install directory is added to my system path when do I echo $PATH, my understanding is that both the CLI and the Python execution are using the same environment. So I don't understand what is the difference in execution?
UPDATE: this seems to be an issue related to PyCharm. I tried executing from VS Code and there were no errors. I am using PyCharm Professional. Does anyone have an idea of what is wrong with my setting in PyCharm?
PyCharm may be using its own shell and so its PATH environment variable may not contain a path to liquidbase binary. Try this.
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!
EDIT
Using VSCode, I had an issue while debugging in a virtual environment that have different packages which are not installed in the base environment. After activating the environment with the command activate my_env, I can use the packages in the environment with usual python command as python main.py. But while debugging, I can't use the packages which are only installed in my_env. How can I debug a python code in a virtual environment using VSCode?
This is this the summary of the question. The rest has some specific info about my case.
BEFORE EDIT
I am trying to use xmltodict package with a simple code using visual studio code.
import xmltodict
with open('C:\\Users\\user\\foo.xml') as f:
db_dict = xmltodict.parse(f.read())
print(db_dict)
I have a virtual environment named my_env, and I installed xmltodict package in it. When I activate the environment with activate my_env, this code works fine. But, when I try to use vscode debug option, it gives No module named 'xmltodict' error. Becuase vscode debug button opens new cmd and run the debugging command in it, I stopped debugging and typed activate my_envin that cmd and tried to debug again, but still it can't find the module. Also, I tried jupiter notebook in vscode, it also doesn't see the package.
I see that import xmltodict is underlined with red in vscode and it says Unable to import 'xmltodict', but it works when I run it normally from cmd. This happens sometimes for other modules and I don't know why. I installed xmltodict module using pip, maybe it causes that.
I am using Visual Studio Code 1.30.1 with Anaconda Python 3.7.1 on Windows 10.
How can I debug a python code in a virtual environment using VSCode?
I saw this question, but I don't think it is exactly what I want?
Make sure the environment you want to use is selected in the Python extension for VS Code by running the Select Interpreter command or via the status bar. Otherwise you can explicitly set the Python interpreter to be used when debugging via the python setting for your debug config.
Use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P) and select the python interpreter that belongs to the new virtual environment.
If you are using a virtual env on linux on the drop down, select env>bin>python i.e env/bin/python
I am using venv for creating virtualenv, and VS code to debug the code.
I found we don't have to create a launch.json file but add settings.json under {project}/.vscode/ folder.
My settings.json is as below:
{
"python.testing.unittestArgs": [
"-v",
"-s",
".",
"-p",
"test_*.py"
],
"python.testing.pytestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.unittestEnabled": true,
"python.pythonPath": "/Users/hhh/project/bin/python"
}
I can debug the project and run the unit test as well.
Hope it will help you.
Two steps for the venv python debug
in the project local settings.json:
"python.pythonPath":"venv/bin/python"
On the left in the debug side panel: Click "create launch.json" below the blue "Run and Config" button
A project local launch.json with python is created:
{ "name": "Python", "type": "python", "request": "launch", "program": "${file}", }
Press F5 and shoot
I am using python 2.7 with flask and using pycharm professional IDE, I am running the flask application using a virtual environment from inside pycharm.
When I open a terminal inside pycharm and use CLI commands, it works, and when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working, the app is still running all the time, and the command is excactly the same.
When I try to activate the same venv outside of the IDE i get permission issue, I assume that it has to do with the venv already being active inside the IDE.
What is the issue? I need to run the same virtual environment in order to use the CLI commands?
How can i access the CLI commands from outside of the IDE?
Thanks
When I try to activate the same venv outside of the IDE I get permission issue
Most likely the problem is all about permission access to the virtual environment's files. Check out access permissions and user:group ownership using ls -al (if you're on Mac or Linux), more info here:
https://linux.die.net/man/1/ls
https://linux.die.net/man/1/chmod
https://linux.die.net/man/1/chown
I assume that it has to do with the venv already being active inside the IDE
Definitely not, you can activate it as many times as you want.
I need to run the same virtual environment in order to use the CLI commands?
At least you have to have all the dependencies installed in your other environment (global or virtual) if you have plans to use one.
when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working
You'd better post a full error output so that we could check the actual error. Also what command are you trying to run?
On windows 10, virtualenv installs but that's where it stops. Using the "one basic command", "virtualenv MyPathandFile", only gives the error: "'virtualenv' is not recognized as an internal or external command".
Tried "python virtualenv" or variations of it doesn't work either. These give the message: "no such file or directory" Are there other packages required? It was installed in an administrator cmd window. Tried uninstalling/reinstalling as well.
And finally, is there another, easier way to make virtual environments?
It sounds like it is not in your path. Can you go to your python installation directory and look for a folder called Scripts? There should be a virtualenv.exe script there. If it is there, add the path to that folder to your path environment variable.
You can also try python -m virtualenv and see what that returns.
See my Scripts folder (notice virtualenv at the bottom):