-1

I want to get the .exe file from a code source but doing python main.py build results in this error:

C:\MyProject>python main.py build
Traceback (most recent call last):
File "main.py", line 5, in <module>
import parserz as parser
File "C:\MyProject\parserz.py", line 9
import * from modbus
       ^
SyntaxError: invalid syntax

Any idea please? Maybe a problem with pip?

Mike
  • 2,637
  • 5
  • 29
  • 40
salwa17
  • 43
  • 1
  • 6

3 Answers3

1

In python you import like this

from modbus import *

Also, In python its good practice to import only what you need.

So you shouldn't use from .... import * instead use

from modbus import something
Arpit Svt
  • 1,153
  • 12
  • 20
1

You can either import the module and run all normal code with

import modbus

or you can import all the classes, functions, variables, etc., from the file to use later in your code with

from modbus import *

To illustrate my point:

If you have two files my_imports.py and main.py that contain the following code:

my_imports.py:

print('Imported module my_imports')

def add_nums(a,b):
    return a+b

def another_function():
    return 'this function was also called'

(version 1) main.py:

import my_imports

# this code would fail because the function isn't imported
print(add_nums(5,7))

(version 2) main.py:

from my_imports import *

print(add_nums(5,7))
print(another_function())
  • In verion 1 of main.py you would see Imported module my_imports in the output but your code would fail when you try to use the add_nums function defined in my_imports.py.
  • In version 2 of main.py you would still see Imported module my_imports in the output but you would also see the result of calling the other two functions in the output as they are now available for use in main.py:
12
this function was also called

As mentioned in some of the other answers, you can also just import the functionality you want from another python script. For example, if you only wanted to use the add_nums method, you could instead have

from my_imports import add_nums

in your main.py.

compuphys
  • 1,289
  • 12
  • 28
  • In which file ? I have 3 files: main.py, parserz.py and modbus.py and I have already add modbus in parserz.py with this line: import * from modbus – salwa17 Mar 31 '20 at 17:36
  • Change `import * from modbus` to `from modbus import *` in your parserz.py file if this is the only place you require the modbus functionality. – compuphys Mar 31 '20 at 17:38
0

Generally from modbus import * should be enough. But it is generally not a good idea to import all so I recommend import modbus as mb. Also you might want to look into modbus libraries like pyModbus or minimalModbus. Here's a good link depicting their pros and cons: Python modbus library

Arnav Motwani
  • 707
  • 7
  • 26