Make python file into a library file

Asked

Viewed 23 times

0

I have a python file that I want to transform into a library so I can call it in another file through the import subprocess, for files with extension . c the command I know is cc -o nomeficheiro nomeficheiro.c but to do this for python I can not find information how to do. What I have been able to do so far is the following:

nome do ficheiro com este código é libraryteste

     #biblioteca para chamar ficheiros de outra extensão
        from subprocess import check_output
        import time
        import sys
        from statistics import mean

        tam=1
        vec=[]

        def Average(lst):
            return mean(lst)

        def criaVetor(value):
            for i in range(tam):
                vec.append(value)
            return vec

        while True:
            sensor_data= check_output(['./raspandmax','-d'], shell=False).decode()# chama o ficheiro raspandmax.c em modo diferencial
            output_value = (sensor_data)# mostra o valor analgico lido
            adc_value = float(output_value)# converte o valor analógico de string para float
            mean_value = Average(criaVetor(adc_value))
            print("{0:.4f}".format(mean_value))
            time.sleep(5)

The file where I import the previous code is as follows:

o nome do ficheiro deste código é sensor_teste
     import libraryteste
        ##### Data of Magnetic Sensor ######################

the problem I’m having is that when I run the sensor_test file it immediately shows the value you’re reading on the sensor and if I try to store that value in a variable it doesn’t do so.

  • Any Python file can be used as a module in another Python file. That is, the import should work as long as the file exists and is in one of the import directories (installed globally in the environment or locally in the application).

  • I was able to import the file only I am not able to save the value in a variable, I will edit the question with the code to understand my question better

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.