Python 3 http.server syntax error - python-3.4

Hi I try to set up my first simple Python http server. This is what I tried. Please help.
>>>import os
>>>os.chdir('/users/ds/my documents/pythonservers/frenchacademy')
>>>os.getcwd()
'C:\\users\\ds\\my documents\\pythonservers\\frenchacademy'
>>> python -m SimpleHTTPServer
SyntaxError: invalid syntax
>>> python -m SimpleHTTPServer 8000
SyntaxError: invalid syntax
>>> -m SimpleHTTPServer
SyntaxError: invalid syntax
>>> python -m http.server
SyntaxError: invalid syntax
>>> -m http.server
SyntaxError: invalid syntax
>>> --version
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
--version
NameError: name 'version' is not defined
>>> python --version
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
python --version
NameError: name 'python' is not defined
>>> $ python -m http.server
SyntaxError: invalid syntax
>>> python3 -m http.server
SyntaxError: invalid syntax
>>> -m http.server
SyntaxError: invalid syntax
>>> python3 -m http.server --help
SyntaxError: invalid syntax

Fund the solution. As usually a stupid error. I had to put the command in the windows command line and not in python itself.

Related

crontab error "ImportError: No module named request" python runs OK in terminal - fails in crontab

I am having difficulty in a running python script that calls module "requests" in crontab. This was fine a few days ago and then I had to change my authentication for Google (to send emails), then "requests" stopped working in crontab. The python script runs fine in a terminal but will not execute in crontab. "requests" is available and when I type "pip3 show requests" the following is displayed (note I replaced my user name with "user"):
$pip3 show requests
Name: requests
Version: 2.27.1
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me#kennethreitz.org
License: Apache 2.0
Location: /home/user/.local/lib/python3.6/site-packages
Requires: certifi, idna, urllib3, charset-normalizer
A simplified version of the python file I would like to execute in crontab is:
#!/usr/bin/env python...
# -*- coding: utf-8 -*-
import requests
print ('End of code')
The file test_request.py executes fine in a terminal.
I created a bash script called test_request.sh based on directions from this stack overflow page:
"ImportError: No module named requests" in crontab Python script
That bash script is this:
#!/bin/bash
echo test_request.sh called: `date`
HOME=/home/user/
PYTHONPATH=/home/user/.local/lib/python3.6/site-packages
cd /home/user/Documents/bjg_code/
python ./test_request.py 2>&1 1>/dev/null
When I try to run the bash script in a terminal or in crontab I receive this error:
$bash test_request.sh
test_request.sh called: Sat Jun 11 14:18:46 EDT 2022
Traceback (most recent call last):
File "./test_request.py", line 4, in <module>
import requests
ImportError: No module named requests
Any advice would be welcomed and appreciated.
Thank you in advance.

How to fix "must be declared in an enclosing class" error in Deno?

I compiled my deno app and on one of my servers I get this error:
$ ./api
error: Uncaught SyntaxError: Private field '#h1' must be declared in an enclosing class
at file://$deno$/bundle.js:3004:16
two other servers it runs fine.
Bother servers are running Ubuntu 20.04.3 LTS
deno -V is 1.15.3
So I had to pass the -r flag for recompiling typescript
$ deno compile --allow-all --unstable -r --no-check --output ./api ./index.ts
it now runs

Retrieve output from a shell call to python in a makefile: is there a missing step?

I have the following makefiles:
# bash version
SHELL:=/usr/bin/env bash
H:=$(shell pwd)
install:
#echo "$H::make.$#: not implemented!"
And a python3 version:
SHELL:=/usr/bin/env python3
H:=$(shell "import os,sys; print(os.getcwd(),file=sys.stdout)")
install:
#print("$H::make.$#: not implemented!")
The bash version works perfectly, whereas the python3 version works, persay. However, the path variable $H is empty.
A check of the python expression in the shell call confirms it is working python code:
python3 -c "import os,sys; print(os.getcwd(),file=sys.stdout)"
//works
How does one comply with the expected behavior in that shell call so that make picks up the output?
That is not what the shell command you wrote actually does.
This:
SHELL:=/usr/bin/env bash
H:=$(shell pwd)
causes the equivalent of:
/usr/bin/env bash -c 'pwd'
to be run.
So, this:
SHELL:=/usr/bin/env python3
H:=$(shell "import os,sys; print(os.getcwd(),file=sys.stdout)")
causes the equivalent of this to be run:
/usr/bin/env python3 -c '"import os,sys; print(os.getcwd(),file=sys.stdout)"'
to be run. A string in Python is just a no-op statement, so this does nothing.
Remove the quotes:
H := $(shell import os,sys; print(os.getcwd(),file=sys.stdout))

How to get Python `unittest` `assertRaises` methods to print `msg` parameter?

In the following, the test should fail and print the msg parameter. But it doesn't.
with self.assertRaises(ZeroDivisionError, msg="Unexpected denominator"):
1/1
This parameter (msg) works in every other assert* method. It seems highly unlikely that something so fundamental could be broken in unittest, so what's the deal?
Here's a complete program that demonstrates the problem:
#!/usr/bin/env python2
import unittest
class TestAssertRaisesMsgParam(unittest.TestCase):
def test_assert_raises_msg(self):
"""
Test unittest `assertRaises` msg param is printed
"""
with self.assertRaises(ZeroDivisionError, msg="Unexpected denominator"):
1/1
if __name__ == '__main__':
unittest.main()
Here's my interaction with it:
$ ls -l assertRaises.py
-rwxr-xr-x 1 tom users 440 Jun 10 14:04 assertRaises.py
$ python -m unittest assertRaises
F
======================================================================
FAIL: test_assert_raises_msg (assertRaises.TestAssertRaisesMsgParam)
----------------------------------------------------------------------
Traceback (most recent call last):
File "assertRaises.py", line 14, in test_assert_raises_msg
1/1
AssertionError: ZeroDivisionError not raised
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
And some system info:
$ /usr/bin/env python2
Python 2.7.13 (default, Mar 22 2017, 12:31:17) [GCC] on linux2
$ uname -a
Linux ... 4.4.62-18.6-default #1 SMP Fri Apr 21 16:14:48 UTC 2017 (84f9824) x86_64 x86_64 x86_64 GNU/Linux
Your code works on my box:
$ python -m unittest tester
F
======================================================================
FAIL: test_assert_raises_msg (test.TestAssertRaisesMsgParam)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\danth\src\test.py", line 12, in test_assert_raises_msg
1/1
AssertionError: ZeroDivisionError not raised : Unexpected denominator
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Make sure you run your test like this:
python -m unittest name_of_test_file

openstack - stack.sh fails on syntax errors

I am trying to install Devstack as non-root user, but getting errors.
The log directory contains only broken symbolic links stack.sh.log and stack.sh.log.summary (pointing to nonexistent files).
I've used the sample local.conf - the only change is that I defined the $DEST.
OS: RHEL 6.6
STDOUT/ERR:
/home/john/scripts/openstack/devstack/functions-common: line 68: conditional binary operator expected
/home/john/scripts/openstack/devstack/functions-common: line 68: syntax error near `"$1"'
/home/john/scripts/openstack/devstack/functions-common: line 68: ` [[ -v "$1" ]]'
./stack.sh: line 119: GetDistro: command not found
/home/john/scripts/openstack/devstack/functions-common: line 68: conditional binary operator expected
/home/john/scripts/openstack/devstack/functions-common: line 68: syntax error near `"$1"'
/home/john/scripts/openstack/devstack/functions-common: line 68: ` [[ -v "$1" ]]'
/home/john/scripts/openstack/devstack/stackrc: line 48: isset: command not found
/home/john/scripts/openstack/devstack/.localrc.auto: line 84: enable_service: command not found
/home/john/scripts/openstack/devstack/stackrc: line 498: is_package_installed: command not found
/home/john/scripts/openstack/devstack/stackrc: line 666: get_default_host_ip: command not found
/home/john/scripts/openstack/devstack/stackrc: line 668: die: command not found
WARNING: this script has not been tested on
./stack.sh: line 179: die: command not found
./stack.sh: line 197: export_proxy_variables: command not found
./stack.sh: line 202: disable_negated_services: command not found
./stack.sh: line 209: is_package_installed: command not found
./stack.sh: line 209: install_package: command not found
[sudo] password for john:
./stack.sh: line 231: is_ubuntu: command not found
./stack.sh: line 238: is_fedora: command not found
./stack.sh: line 301: safe_chown: command not found
./stack.sh: line 302: safe_chmod: command not found
./stack.sh: line 310: safe_chown: command not found
Traceback (most recent call last):
File "/home/john/scripts/openstack/devstack/tools/outfilter.py", line 24, in <module>
import argparse
ImportError: No module named argparse
First, fix the missing module by using yum:
yum install python-argparse.noarch
Also you will need to run ./unstack.sh to clear the logs.
I had still faced this issue, so further debugging lead me to an issue when both python-zaqarclient and python-openstackclient were installed. As a quick solution I removed python-zaqarclient:
sudo pip uninstall python-zaqarclient
Then
- apt-get upgrade
- apt-get dist-upgrade
- ./stack.sh
Hope this helps!

Resources