i have a python3.6.0 script which works perfectly well on windows. but when i run it in bash i get syntax error. here is the code:
import itertools
lines=["Hamlet","William Shakespeare","Edited Barbara B Mowat Paul Werstine","Michael Poston Rebecca Niles","Folger Shakespeare Library","httpwwwfolgerdigitaltextsorgchapter5playHam","Created Jul 31 2015 FDT version 092","Characters Play","line 17 POLONIUS father Ophelia Laertes councillor King Claudiusthis line substituted GHOST"]
LinesMap=dict()
for line in lines:
list=[l for l in line.split(' ')]
d = dict(itertools.zip_longest(*[iter(list)] * 2, fillvalue=None))
LinesMap = {**LinesMap, **d}
print(LinesMap)
this is the error:
[reza#localhost ~]$ python New\ Text\ Document.py
File "New Text Document.py", line 16
LinesMap = {**LinesMap, **d}
^
SyntaxError: invalid syntax
On many Linux distros, python refers to Python 2, and you need to use python3 to run a Python 3 script.
Depending on your distro, you should be able to yum or apt-get a package with Python 3, which can coexist nicely alongside Python 2, partly thanks to the decision to use different names for the executables.
In more detail, apt-get install -y python3 (probably with sudo or ask your admin) should install Python 3 on Debian-based platforms (Mint, Ubuntu, what have you); and yum install python36 should install Python 3.6 on various RPM-based platforms (Fedora, CentOS, etc, again probably with sudo or something equivalent).
Try
python3 New\ Text\ Document.py
Bash is using python 2 to run this.
Related
I am new to transcrypt. I created a test python file,test.py
def test_def(a: list):
for i in range(len(a)):
print(i)
xx = [2, 3, 4]
test_def(xx)
I have python 3.9. If I run the python file, it runs and prints as expected.
Running transcrypt gives following error
> python -m transcrypt -b -m -n .\test.py
Error while compiling (offending file last):
File 'test', line 2, namely:
Error while compiling (offending file last):
File 'test', line 2, namely:
Aborted
I am not sure what it expects and why is it giving the error, any help would be appreciated.
What version of Transcrypt are you using? Wasn't able to replicate the error using Python 3.9.0 and Transcrypt 3.9.0. You can check like this (Win):
> transcrypt -h | find "Version"
# and may as well double check that the right version of python was used:
> python --version
The Python and Transcrypt versions should match, as Transcrypt uses Python's AST that may change between versions.
Another aspect is that I first installed Transcrypt into a virtual env as follows (Win):
> py -3.9 -m venv wenv
> wenv\Source\activate.bat
> pip install transcrypt
> python -m transcrypt -b -m -n test.py
It sometimes happens that one inadvertently uses the wrong version of Python. 'py' is the Python launcher on Windows and useful to launch the right version if you have multiple versions installed. On Linux there are typically binaries in /usr/bin such as 'python3.9' or a symlink such as 'python3' that links to the most recent 3 version. Installing into a virtual env as demonstrated is also helpful, as various problems can come about otherwise.
The above compiled test.py without error (Win 10 cmd.exe). Fwiw, the file was saved as utf-8 and compile worked with and without a BOM.
Recently I have been learning python and I have just started a project making a nmap scanning script that will scan a network range for open ports. My only problem is that I have installed the nmap module but in my code, it says that the module does not have the attribute port scanner when I attempt to run it. I have looked around quite a bit for a solution and I have seen a lot of different solutions I will list what I have tried here.
First was to install python-nmap that didn't work
Next was to install the module nmap this didn't work either
I also heard you should uninstall nmap and install python-nmap instead this also did not work
Next It was suggested that since I am using python3 I should use pip3 to install python-nmap that didn't work either
Next I tried manually downloading and installing it and putting it in /lib/python3.7/dist-packages this didn't seem to do anything
I did notice that even though the installation did not fail on any of these methods only the manual install made it show up in the dist-packages folder and it still did not work after this. None of my versions of python have the nmap module inside of them. Does anyone know anything else that I should try to make this work? I am currently running a Debian 9 based os. thanks for your help in advance.
this is the error
Cannot find reference 'PortScanner' in '__Init__.py'
this is my code
import nmap
ns = nmap.PortScanner()
Basically you have to install nmap:
sudo apt-get install nmap
Then install python module:
sudo pip3 install -U python-nmap
After this you're good to go:
>>> import nmap
>>> nm = nmap.PortScanner()
>>> nm.scan('127.0.0.1', '22-443')
>>> nm.command_line()
'nmap -oX - -p 22-443 -sV 127.0.0.1'
>>> nm.scaninfo()
{'tcp': {'services': '22-443', 'method': 'connect'}}
>>> nm.all_hosts()
['127.0.0.1']
>>> nm['127.0.0.1'].hostname()
'localhost'
>>> nm['127.0.0.1'].state()
'up'
>>> nm['127.0.0.1'].all_protocols()
['tcp']
>>> nm['127.0.0.1']['tcp'].keys()
[80, 25, 443, 22, 111]
I am using a bot with python to create accounts on a website. When I use it on my windows machine with python installed, it works fine. I bought a linux VPS yesterday (Ubuntu 16.04) and my script apparently isn't working anymore. This is the error I get on my linux machine:
Traceback (most recent call last):
File "roblox.py", line 2, in <module>
from utils import *
File "/home/py/newbot/utils.py", line 18
key, *values = line.split(" ")
^
SyntaxError: invalid syntax
The line in the script its referring to is this
def string_to_dict(headers):
headers_dict = {}
for line in headers.split("\n"):
if not line: continue
line = line.strip()
key, *values = line.split(" ")
key = key[:-1]
if not (key and values): continue
headers_dict[key] = " ".join(values)
return headers_dict
Any ideas what could have gone wrong?
This syntax is invalid in python 2. Run your script with python3.
As mentioned in other answers, you need to run this in Python3. The shortest answer is:
Install python3, which may not already be on your machine: $ sudo apt-get update; sudo apt-get install python3
Get in the habit of using a shebang in your script files. Add #!/usr/bin/env python3 to the first line of your script to direct the shell to invoke python3 instead of python2 -- most systems come with /usr/bin/python aliased to python2 installations.
You can find out more about sourcing files in the Bash shell (default shell for Ubuntu 16.04) here.
The longer answer -- if you're wondering why the code doesn't work in Python2 but does work in Python3 -- is that Python3 introduced extended iterable unpacking in PEP 3132, but this feature was not implemented in Python2.
I installed Python 3.4.2 and Virtualenv 12.0.5 in my Linux Mint 17.1
Then I tried creating:
$ virtualenv venv
And also using --clear and/or -p /usr/bin/python3.4, always getting the messages:
Using base prefix '/usr'
New python executable in venv/bin/python3
Also creating executable in venv/bin/python
ERROR: The executable venv/bin/python3 could not be run: [Errno 13] Permission denied
Another try was:
$ pyvenv-3.4 venv
It gave no errors on creation, but in the venv/bin file the python3.4 is a symbolic link to /usr/local/bin/python3.4. Then when I activate and install any lib using pip or pip3, then try to import it, I get the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'anymoduledownloaded'
I always used virtualenv in Python 2.X and never got this kind of errors. Any thoughts on what am I doing wrong?
Thanks!!
=======EDITED=======
This is the output of my partitions (fdisk -l):
Device Boot Start End Blocks Id System
/dev/sda1 2048 98707455 49352704 83 Linux
/dev/sda2 303507456 3890644991 1793568768 5 Extended
/dev/sda3 * 98707456 303507455 102400000 7 HPFS/NTFS/exFAT
/dev/sda4 3890644992 3907028991 8192000 82 Linux swap / Solaris
/dev/sda5 303509504 3890644991 1793567744 7 HPFS/NTFS/exFAT`
And also my fstab:
<file system> <mount point> <type> <options> <dump> <pass>
-> was on /dev/sda1 during installation
UUID=a38f9c6d-3cd9-4486-b896-acbc6182ec61 / ext4 errors=remount-ro 0 1
-> swap was on /dev/sda4 during installation
UUID=efad7b53-79a8-4230-8226-9ca90c68ea9d none swap sw 0 0`
Is that a shared partition that you have mounted? Does the shared partition have a different filesystem then the non-shared one you tried upon? If yes, then IMO, that will definitely cause an error since you are making and compiling binaries for python on one filesystem, and so it will not work on another filesystem.
As mentioned in this answer, adding to your /etc/fstab with an entry with exec flag might make it work for you, i.e., you might need to add another entry for the NTFS disk here to make it automount:
<file system> <mount point> <type> <options> <dump> <pass>
-> was on /dev/sdaX during installation
UUID=<uid_of_NTFS> / ntfs auto,user,exec,nodev,rw,errors=remount-ro 0 1
I struggled with this as well so I wrote an ugly bash script to help me with this. The only salient difference between what you do and what I do is on line 133:
/path/to/python/bin/python3.4 /path/to/python/bin/pyvenv /path/to/venv
That is, explicitly name the instance of python and the venv tool. Then
/path/to/venv/bin/pip install django # or whatever
Edit
I installed Linux Mint in a VM to try and build a Python 3.4 virtual environment. Based on the error messages I saw and this answer, I learned that I must do the following to get a complete Python 3.4 build:
apt-get install build-essential libssl-dev openssl
Without this, my Python 3.4 build did not contain pip. Note that you probably want to install readline and other development packages.
Unsolicited Advice
Do not do this as root, create a user dedicated to running your venv
Create a script to create your environment
Check that script into your source code repo
I deleted my python binaries and venvs multiple times and then recreated all of with this script to make sure that my script reproduced my environment and then stripped the identifying information and saved that on github to share it. I should really be using a more formal tool for this like docker/puppet/chef.
I am trying to use google protocol buffer in windows with python binding, however I meet some problem during the install step.
Follow the instruction, I have to compile the PB myself using vs, but I have no vs installed on my machine, then I found a window binary at the download page.
Also I download the full source code package, then I put the protoc-2.5.0-win32.zip\protoc.exe to C:\windows\system32.
Then I go to the protobuf-2.5.0.zip\python and run the python setup.py install to install the python binding.
However I get the error like this:
And when I check the directory, the file google\protobuf\compiler does not exist.
What's the problem?
Is it possible to use it without compiling?
I was just suffering from the same problem. The solution is to explicitly do the build step before.
python setup.py build
python setup.py install
That worked for me.
Before this package can be installed on windows you need to download the compiler (protoc.exe) and put it in the environment execution path.
After this step is done you can run:
python setup.py build
python setup.py install
~yy
C:\Users\dev3\protobufcode\protobuf-master\python
$ python setup.py build
$ python setup.py install
C:\Users\dev3\protobufcode
$ python --version
Python 3.6.5
$ python -i build\gen\addressbook_pb2.py
>>> import addressbook_pb2
>>> Person = addressbook_pb2.Person()
>>> person = addressbook_pb2.Person()
>>> person.id = 1234
>>> person.name = "Go Fish"
>>> person.email = "gofish#example.com"
>>> phone = person.phones.add()
>>> phone.number = "111-2222"
>>> phone.type = addressbook_pb2.Person.HOME
>>> person
name: "Go Fish"
id: 1234
email: "gofish#example.com"
phones {
number: "111-2222"
type: HOME
}
>>>