-1

If I type on the python console these three lines:

>> import serial 
>> s = serial.Serial('/dev/tty.usbmodemFD121', 9600)
>> s.write('00001§1\r')

Everything works perfectly! But if i put these three lines inside a script so:

# -*- coding: utf-8 -*-
import serial 
s = serial.Serial('/dev/tty.usbmodemFD121', 9600)
s.write('00001§1\r')

And I run it from the bash:

/Users/francesco/sender.py 

Doesn't work! How is possible? Someone can help me?

Francesco Scala
  • 201
  • 3
  • 15

1 Answers1

2

you can't run python files directly like that. Have you tried:

python /Users/francesco/sender.py 
Hongyu Wang
  • 378
  • 1
  • 10
  • 1
    But you can run them directly if you `chmod +x` and add `#!/usr/bin/env python` as the first line. – cxw Jul 06 '18 at 20:12
  • 1
    @cxw That's going to get you in trouble more than it's worth. It might work on your local machine, but trying to run that in a different environment is going to cause you problems. Python files aren't meant to be run like that afaik. – BowlingHawk95 Jul 06 '18 at 20:14
  • @BowlingHawk95 it works fine in linux, and on windows the file associations system mean that you can run them without python prefix even if there isn't a shebang. But actually the shebang causes issues in python 3 (with `py` wrapper trying to interpret it) – Jean-François Fabre Jul 06 '18 at 20:17
  • ^ little gotchas like that are what I mean. It might work on some environments, but kind of the point of using an interpreted language like python is to minimize the dependencies on the execution environment. Python files should be treated as text, not executables. – BowlingHawk95 Jul 06 '18 at 20:19
  • the script is executed but I not receive the string on the serial device – Francesco Scala Jul 06 '18 at 20:23