gTTS(Google文本到语音)模块的python代码错误

时间:2018-11-04 06:45:43

标签: python valueerror

当我从

输入命令时
.gtts

它带有一个错误代码

  

ValueError:尝试以非包形式进行相对导入

我已经尝试修复gtts一段时间了,就像收到错误代码

之前一样
  

没有名为“ gtts”的模块

但是我已经安装了它。所以我去找了一下,发现如果我使用FusedLocationProviderClient client; TextView textView; boolean started = false; double lat = 0; protected static final int REQUEST_CHECK_SETTINGS = 0x1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text); client = LocationServices.getFusedLocationProviderClient(this); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.e(tag, "no perm"); ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 1); } else work(); } void work() { if (!isLocationEnabled()) createLocationRequest(); else locationDetails(); } int t = 0; @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.e(tag, "activity result"); work(); } void locationDetails() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } client.getLastLocation() .addOnSuccessListener(this, new OnSuccessListener<Location>() { @Override public void onSuccess(Location location) { if (location != null) { if(lat!=0)return; lat = location.getLatitude(); textView.setText("location: lat" + lat + " lng" + location.getLongitude()); Log.e(tag, "happy"); } else { repeatedRequests(); Log.e(tag, "null location"); } } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION}, 1); } else work(); } void createLocationRequest() { final LocationRequest mLocationRequest = LocationRequest.create(); mLocationRequest.setInterval(10000); mLocationRequest.setFastestInterval(5000); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(mLocationRequest); final SettingsClient settingsclient = LocationServices.getSettingsClient(this); Task<LocationSettingsResponse> task = settingsclient.checkLocationSettings(builder.build()); task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() { @Override public void onSuccess(LocationSettingsResponse locationSettingsResponse) { repeatedRequests(); Log.e(tag, "success"); } }); task.addOnFailureListener(this, new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { if (e instanceof ResolvableApiException) { Log.e(tag, "failure" + e.getLocalizedMessage()); // repeatedRequests(); try { // Show the dialog by calling startResolutionForResult(), // and check the result in onActivityResult(). ResolvableApiException resolvable = (ResolvableApiException) e; resolvable.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException sendEx) { Log.e(tag, "erorr " + sendEx.getLocalizedMessage()); // Ignore the error. } } } }); } void repeatedRequests() { if (!started) { started = true; Log.e(tag, "Thread started"); new Thread(new Runnable() { @Override public void run() { while (lat == 0) { try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } t++; if (t > 50) break; Log.e(tag, "repeat"); runOnUiThread(new Runnable() { @Override public void run() { locationDetails(); } }); } } }).start(); } } boolean isLocationEnabled() { int locationMode = 0; try { locationMode = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE); } catch (Settings.SettingNotFoundException e) { e.printStackTrace(); return false; } return locationMode != Settings.Secure.LOCATION_MODE_OFF; } ,它将提供另一个我认为比我上次得到的错误代码更好的错误代码

1 个答案:

答案 0 :(得分:1)

from gtts import gTTS
myobj = gTTS(text="Hello', lang='en')
myobj.save('hello.mp3')

以上应该有效。 gtts版本应为1.1.2