-1

thanks for helping me!

I just set up google cloud speech and tried to run the code in "Make an audio transcription request" in this website: Quickstart: Using Client Libraries

the code is shown below after I open ubuntu terminal:

export GOOGLE_APPLICATION_CREDENTIALS="/home/luffy/Lumariatuan-xxxxxxxxxxxx.json"

python

import io

import os

from google.cloud import speech

from google.cloud.speech import enums

from google.cloud.speech import types

client = speech.SpeechClient()

# The name of the audio file to transcribe

file_name = os.path.join(
    os.path.dirname(__file__),
        'resources',
        'audio.raw')

However, when I run the last part os.path.dirname(__file) in Ubuntu 18.04 terminal in python, the error happens like below:

* NameError: name '__file__' is not defined*

How can I solve the problem?

LU Huang
  • 11
  • 5
  • Can you see if [this](https://stackoverflow.com/questions/16771894/python-nameerror-global-name-file-is-not-defined#20510502) . `__file__` needs to be in a file – Madhan Varadhodiyil Jul 29 '18 at 15:21
  • 3
    you can't do this in terminal, try to use it in .py file – dilkash Jul 29 '18 at 15:27
  • Thank you @MadhanM and @dilkash! I put all the code in a .py file, and I solve the problem! However, another problem occurs when I keep running the code. Could you halp me look at it? Thanks! It's here: [Problem](https://stackoverflow.com/questions/51586920/bad-filenotfounderror-errno-2-no-such-file-or-directory) – LU Huang Jul 30 '18 at 04:21
  • 1
    @MadhanM or @ dilkash could you please post your comment as an answer so it can be accepted by LUHuang? – Federico Panunzio Jul 30 '18 at 10:17

1 Answers1

0

The file variable contains the path to the file that Python is currently importing. You can use this variable inside a module to find the path of the module. It'll not work with an interactive python terminal.

Madhan Varadhodiyil
  • 1,932
  • 1
  • 11
  • 16