I am running a Django server from my run configuration which translates to:
python manage.py runserver --noreload
The stop button in the error panel doesn't kill the process, so I usually run
pskill python
How do I add this to my Eclipse run configurations?
My solution was to create a python file named "pskill.py" containing:
from subprocess import call
from sys import argv
if __name__=='__main__':
call(['pskill', argv[1]])
And add it under "Python Run" in Run Configurations.
Related
Whenever I try to run Anaconda Navigator (or Spyder), either from cmd or from windows, it does not launch. My mousepointer briefly shows that it is loading, but then nothing happens.
I have tried a number of solutions from other posts, but nothing worked:
I have completely uninstalled and reinstalled Anaconda
I have tried conda update -n root conda + conda update --all
I have tried conda clean --packages && conda clean --all && conda update --all
I have tried anaconda-navigator --reset
and a few more things.
My issue looks as follows:
Not a solution by itself, but: PyQt5 is bundled with Anaconda by default, so you shouldn't need to install it (again). In order to verify the presence of PyQt5:
a) execute conda list in the base environment and search for the package pyqt.
b) launch Spyder in the base environment and execute the following code:
from PyQt5.QtWidgets import QApplication, QLabel
app = QApplication([])
label = QLabel("Hello World!")
label.show()
app.exec_()
This should open an application window with the text "Hello World!".
Both would indicate, that PyQt5 is installed, and that the problem lies elsewhere.
Credit goes to JKSH for his answer to ImportError: DLL load failed while importing QtCore: The specified module could not be found.
I've just created my first Python package using Poetry using the usual poetry new mypackage command. My problem is that pytest does not execute any test when I run it. I'm developing using VSCode and the weird behavior is that VSCode successfully finds and executes my tests.
Poetry created a subdir called mypackage and another called tests. My test file is called tests/test_mypackage.py.
VSCode autodiscoverd the tests, and display them in the test tab. The .vscode/settings.json file has this configuration:
"python.testing.pytestArgs": [
"tests"
],
I've tried the following commands to execute pytest:
With my venv manually activated:
pytest
pytest tests
pytest tests/test_mypackage.py
cd tests;pytest test_mypackage.py
without my venv activated:
poetry run pytest
poetry run pytest tests
The behavior is always the same: nothing happens, as if pytest couldn't detect anything to run.
I've been using VSCode to run the tests, but now I want to put the code under Continuous Integration. How do I run pytest to validate my package?
UPDATE: from inside the virtual env pytest does not prints any output when run, but its return code is 1.
Poetry doesn't require to activate the venv. The command should work:
poetry run pytest
Though, you can activate the venv (with poetry shell) and run just pytest.
For the Test Explorer, I think this is still on development, officially, only Pytest and Unittest are supported, but the VSCode suggested a variable to support Poetry.
Here are the configurations that have worked to me:
{
"python.testing.cwd": ".",
"python.poetryPath": "poetry",
"python.testing.pytestEnabled": true,
"python.testing.pytestPath": ".venv/bin/pytest",
"python.testing.autoTestDiscoverOnSaveEnabled": true,
}
If you additionally needs to use environment variables, set them up in a .env file (it would be better a .env.test, but I couldn't figure it out).
pytest still does not work when running within the activated virtual environment, but I discovered that I can execute it running:
poetry run pytest
I still do not understand why it can't find the test when directly run from the command line even with the venv activated.
I am running test discovery in vscode with pytest in the output shows me this command:
python /home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/dave/PythonProjects/pytest_test -s --cache-clear .
Executing this command in the vscode shell leads to an import error.
dave#dave-desktop ~/PythonProjects/pytest_test master python /home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /home/dave/PythonProjects/pytest_test -s --cache-clear .
Traceback (most recent call last):
File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/run_adapter.py", line 17, in <module>
from testing_tools.adapter.__main__ import parse_args, main
File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/__main__.py", line 9, in <module>
from . import pytest, report
File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/pytest/__init__.py", line 7, in <module>
from ._discovery import discover
File "/home/dave/.vscode/extensions/ms-python.python-2020.6.88468/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 8, in <module>
import pytest
ImportError: No module named pytest
Why does this show an error? Is this command executed from another directory. This is very frustrating, because i have no chance of analysing the error this way.
You are probably actually invoking different Python interpreters/environments in each methods. VSCode docs say:
By default, the Python extension looks for and uses the first Python interpreter it finds in the system path.
But there are several ways of overriding this, manually selecting the interpreter is the most obvious.
To debug, I suggest you temporarily put the following lines at the very top of run_adapter.py (might wanna back it up first, as it is part of the VS-code Python extension):
import sys
print(f'interpreter: {sys.executable}')
This will print the path of your interpreter just before the import fails.
You might also want to take a look at this question.
I couldn't see if in the comments above you answered whether the virtual environment was activated or not. It could happen that VSCode is executing the command from a place where the python executable and the module pytest both exist.
One option would be to install pytest globally and see if that helps (you can uninstall it later to clean your system, if needed).
Otherwise, be sure to create a virtual environment, activate it and install pytest on it before running your command.
python3 -m venv /path/to/new/virtual/environment (e.g. ./venv)
./venv/bin/activate
pip3 install pytest
Also, if you have Python 2 and 3 in the same environment, that could cause conflicts. Can you check whether the correct version is used by VSCode (3 from your example, I guess)? You can do Ctrl + Shift + p and select "Python: Select Intepreter" to select the correct version.
I have tried running a ProcessImage.py file in which I import the package pytesseract in Jupiter Lab and VSCode.
This is the error that pops out :
import pytesseract
ImportError: No module named pytesseract
I already know that pytesseract is installed in my environment because with conda list:
pytesseract 0.3.2 py_0 conda-forge
pytest 5.3.5 py38_0 coda-forge
However, if I run my ProcessImage.py file on my local no error is prompted.
I know the error is related to paths in Jupiter Lab and VsCode but I can't seem to find a solution.
Have you tried to launch your python file using Ctrl+F5 command for run?
I had this same ImportError when trying to launch my python program using the .run extension. It persistently started python 2.7 while my environment was using Python3. I first thought that it was a VS code error but it wasn't.
I am trying to run the apache beam program using Python SDK on my local machine. I created and activated the python virtual environment and also installed apache beam using pip.
However, when I trigger the code using the below command, it gives the attribute error and says:
module filename.py has no attribute __path__
Below is the command I ran (by going to the venv folder):
python -m filename.py
Please help me out on this.. I am trying to learn apache beam
import apache_beam as beam
p=beam.Pipeline()
lines= p | beam.io.ReadFromText('path\\My_sample_input_file.txt');
lines | beam.io.WriteToText('path\\output2.txt')
You shouldn't use the -m flag to run a script. See What is the purpose of the -m switch?
To run a python script:
python myfile.py
To import and run a module:
python -m myfile
This will also work because the current working directory is in the search path for python modules.