Hydrogen passes "-f" to ArgumentParser - ipython

I am trying to run the following code in Atom's Hydrogen package
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
which produces the following error
ipykernel_launcher.py: error: unrecognized arguments: -f
Any ideas why this might be happening and how to fix it?

Seem to be related to this issue.
Simple workaround is running import sys; sys.argv=['']; del sys;.

Related

Change syntax of -k option in pytest 7.1.0 is not working with pytest_addoption(parser)

Below is my code which was working perfectly fine till I upgraded pytest to version 7.1.0
pytest.main(self.pytest_args + ["-k", tc_cfg.name, "--aq-cfg", tc_cfg.aq_cfg])
and in conftest, I have below code:
def pytest_addoption(parser):
parser.addoption("--aq-cfg", required=False)
This was working fine for earlier pytest versions. With latest one(pytest 7.1.0) its failing with below error:
ERROR: usage: aqueduct [options] [file_or_dir] [file_or_dir] [...]
aqueduct: error: unrecognized arguments: --aq-cfg all_envs
inifile: /home/aquser/dev/aqueduct/src/cb/test/tools/aqueduct/pytest.ini
rootdir: /home/aquser/dev/aqueduct/src/cb/test/tools/aqueduct
This is occurring because latest change added for -k syntax: https://docs.pytest.org/en/stable/changelog.html#pytest-7-1-0-2022-03-13
But if I try to remove --aq-cfg, its failing at pytest_adoption(parsar) and not able to locate file without "--".
Any help on this would be appreciated!

Importing python modules using sublime text3

I'm learning Python 3.7, and Sublime Text3. Most programs run fine using control + b, but when I try to import a module from the standard library like random, I get the following error(Please note that I am not running the program from OneDrive, but from my own computer!)
Traceback (most recent call last):
File "C:\Users\chris\OneDrive\Documents\Python Programs\guess.py", line 1, in <module>
import random
File "C:\Users\chris\OneDrive\Documents\Python Programs\random.py", line 1
>>> import os
^
SyntaxError: invalid syntax
Obviously the OneDrive thing is a problem ,but what can I do?
Many thanks.
The part of the error message that you want to focus on here is the last part of the stack trace, which is showing you the line that has the problem and where the problem is, i.e.:
>>> import os
^
SyntaxError: invalid syntax
Based on this, your input file has the characters >>> in front of the import statement which, as the message indicates, is not valid Python syntax. Removing those characters should solve the problem for you. The line immediately above the error is showing you the name of the file that has the problem (in this case random.py).
The >>> is the prompt that the interactive Python interpreter gives you when you run it, as it's prompting you for what text to enter. That's not something that should appear in your code at all.
Check the random.py file, as the error message states, it's a syntax error due to the the >>> repl symbol in there.

Failed building wheel in Google ML engine

I follow this tutorial to submit machine learning job to Google ML engine. Then I faced the error ImportError: No module named matplotlib.pyplot but solved it by adding the Matplotlib into RequiredPackages in setup.py. Then I face another error which is ImportError: No module named _tkinter, please install the python-tk package. I found this solution and this solution but do not help and give me another error.
Failed building wheel for my-package
Command "/usr/bin/python -u -c "import setuptools, tokenize;file='/tmp/pip-T6kjZl-build/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-4LpzWh-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-T6kjZl-build/
Command '['pip', 'install', '--user', '--upgrade', '--force-reinstall', '--no-deps', u'my-package-0.1.1.tar.gz']' returned non-zero exit status 1
My setup.py.
"""Setup script for object_detection."""
import logging
import subprocess
from setuptools import find_packages
from setuptools import setup
from setuptools.command.install import install
class CustomCommands(install):
def RunCustomCommand(self, command_list):
p = subprocess.Popen(
command_list,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
# Can use communicate(input='y\n'.encode()) if the command run requires
# some confirmation.
stdout_data, _ = p.communicate()
logging.info('Log command output: %s', stdout_data)
if p.returncode != 0:
raise RuntimeError('Command %s failed: exit code: %s', (command_list, p.returncode))
def run(self):
self.RunCustomCommand(['apt-get', 'update'])
self.RunCustomCommand(
['apt-get', 'install', '-y', 'python-tk'])
install.run(self)
REQUIRED_PACKAGES = ['Pillow>=1.0', 'Matplotlib>=2.1']
setup(
name='object_detection',
version='0.1',
install_requires=REQUIRED_PACKAGES,
include_package_data=True,
packages=[p for p in find_packages() if
p.startswith('object_detection')],
description='Tensorflow Object Detection Library',
cmdclass={
'install': CustomCommands,
})
The tutorial blog link that you are following is outdated I think. I followed this one
you need to change runtime to 1.9 instead of 1.8 in cloud ml training command
From tensorflow/models/research/
gcloud ml-engine jobs submit training `whoami`_object_detection_pets_`date +%m_%d_%Y_%H_%M_%S` \
--runtime-version 1.9 \
--job-dir=gs://${YOUR_GCS_BUCKET}/model_dir \
--packages dist/object_detection-0.1.tar.gz,slim/dist/slim-0.1.tar.gz,/tmp/pycocotools/pycocotools-2.0.tar.gz \
--module-name object_detection.model_main \
--region us-central1 \
--config object_detection/samples/cloud/cloud.yml \
-- \
--model_dir=gs://${YOUR_GCS_BUCKET}/model_dir \
--pipeline_config_path=gs://${YOUR_GCS_BUCKET}/data/faster_rcnn_resnet101_pets.config
after changing runtime everything worked perfect. No need to change any setup file. No Library errors.

How to solve unrecognized arguments error?

The problem is that I can not get through the first step of running the simplest command. When I write this code
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
an then i get this error
usage: main.py [-h]
main.py: error: unrecognized arguments: -f
C:\Users\Saeid\AppData\Roaming\jupyter\runtime\kernel-301e1312-128e-4c4d-9ae8-
035b05a69a59.json
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
C:\Program Files\Anaconda3\lib\site-
packages\IPython\core\interactiveshell.py:2889: UserWarning: To exit: use
'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
The argparse module is used to parse command line arguments. Hence, it doesn't make much sense to do so in an IPython or Jupyter notebook. The error propably stems from the fact that the notebook was invoked with an -f option.
As funky-future pointed out, you should not be using an IPython notebook with argparse. To test how it works, let's assume a file named test.py with the following content:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.echo)
Now in your terminal (cmd or PowerShell) you go to the directory with the test.py file and type:
python test.py 123
The output should be:
123
Source
To avoid that error message, you can do
import argparse
parser = argparse.ArgumentParser()
parser.parse_args([])

unregistered task type import errors in celery

I'm having headaches with getting celery to work with my folder structure. Note I am using virtualenv but it should not matter.
cive /
celery_app.py
__init__.py
venv
framework /
tasks.py
__init__.py
civeAPI /
files tasks.py need
cive is my root project folder.
celery_app.py:
from __future__ import absolute_import
from celery import Celery
app = Celery('cive',
broker='amqp://',
backend='amqp://',
include=['cive.framework.tasks'])
# Optional configuration, see the application user guide.
app.conf.update(
CELERY_TASK_RESULT_EXPIRES=3600,
)
if __name__ == '__main__':
app.start()
tasks.py (simplified)
from __future__ import absolute_import
#import other things
#append syspaths
from cive.celery_app import app
#app.task(ignore_result=False)
def start(X):
# do things
def output(X):
# output files
def main():
for d in Ds:
m = []
m.append( start.delay(X) )
output( [n.get() for n in m] )
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
I then start workers via (outside root cive dir)
celery -A cive worker --app=cive.celery_app:app -l info
which seems to work fine, loading the workers and showing
[tasks]
. cive.framework.tasks.start_sessions
But when I try to run my tasks.py via another terminal:
python tasks.py
I get the error:
Traceback (most recent call last):
File "tasks.py", line 29, in <module>
from cive.celery_app import app
ImportError: No module named cive.celery_app
If I rename the import to:
from celery_app import app #without the cive.celery_app
I can eventually start the script but celery returns error:
Received unregistered task of type 'cive.start_sessions'
I think there's something wrong with my imports or config but I can't say what.
So this was a python package problem, not particularly a celery issue. I found the solution by looking at How to fix "Attempted relative import in non-package" even with __init__.py .
I've never even thought about this before, but I wasn't running python in package mode. The solution is cd'ing out of your root project directory, then running python as a package (note there is no .py after tasks):
python -m cive.framework.tasks
Now when I run the celery task everything works.