I am having an issue with running the module "translate" using a script.
from translate import Translator
import requests
translator = Translator(from_lang = "zh", to_lang="en")
translation = translator.translate("""猗與那與、置我鞉鼓。
奏鼓簡簡、衎我烈祖。
湯孫奏假、綏我思成。
鞉鼓淵淵、嘒嘒管聲。
既和且平、依我磬聲。
於赫湯孫、穆穆厥聲。
庸鼓有斁、萬舞有奕。
我有嘉客、亦不夷懌。
自古在昔、先民有作。
溫恭朝夕、執事有恪。
顧予烝嘗、湯孫之將""")
print(translation)
The strange thing is that the script runs if I copy the code line by line into IDLE. However, if I were to run the script, I get the following message
ImportError: cannot import name 'Translator'
Am I missing something?
Thanks in advance.
What's the name of the file that contains your code? If it's the same as the library you're trying to import (i.e. translate) then python will throw this error since python cannot differentiate between the file and the library names.
Related
Consider the following Python files
file_one.py:
text = ("Sample text")
print(text)
file_two.py:
import file_one
Running python file_two.py gives the following output:
Sample Text
I was wondering why file_two automatically prints the output of file_one when it has been imported. I thought you may need to specifically call it to print out the text like print(file_one.text).
Similarly, in theory, is this any different from importing libraries such as random or pandas etc? As in, if they have a line that says print("hello"), hello will be printed automatically in the output of the module that imports it?
It’s because files are run when imported.
This might help
https://www.pythonmorsels.com/importing-module-runs-code/
When you import a file, everything at the top level of the file (that isn't part of a class or function) is run immediately.
That includes imports of other modules.
I have been working at this for a while, but I have not been making a ton of progress, and I want to know how to import a python file into another, and with the help of a couple websites and debugging I have gotten to this code: import (__file__ + "\Assets\Minigames\minigame1"). I am getting this error in VSCode: Expected module namePylance Statements must be separated by newlines or semicolonsPylance
Currently working with the example script found on IBM Watson's GitHub:
Link: https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/text_to_speech_v1.py
When I run the script, it works perfectly creating the WAV file. However, when I try to play it back within the script, it simply runs and never plays. I tried using PyAudio, Os, Subprocess, and other third party libraries to play the file, however, nothing worked. Is there something I would have to do to the file first before attempting to play it in the script? I'm assuming it has something to do with it being written in binary, which is what the script calls for, but I'm still too new at programming to understand how to solve the problem.
I'll attach my full script below with placeholders for personal info. Thanks!
# coding=utf-8
from os.path import join, dirname
from watson_developer_cloud import TextToSpeechV1
from watson_developer_cloud.websocket import SynthesizeCallback
import subprocess
service = TextToSpeechV1(url='EXAMPLE URL TO API', iam_apikey='EXAMPLE API KEY')
with open(join(dirname(__file__), '..EXAMPLE PATH../resources/output2.wav'),'wb') as audio_file:
response = service.synthesize("What's the weather?", accept='audio/wav', voice="en-US_MichaelVoice").get_result()
audio_file.write(response.content)
def audio_call():
audio_file_path = "..EXAMPLE PATH../resources/output2.wav"
return subprocess.call(["afplay", audio_file_path])
audio_call()
[SOLVED]: Apparently there was a problem with my file directory playing WAV files. By changing the file acceptance to "accept = 'audio/wav'" it worked fine.
I tried installing Yara 3.8.1 with androguard module. During the installation, I faced this issue, so I applied the patch given by #reox to the androguard.c file and it solved the problem. After that I tried a simple Yara rule with import "androguard" using command-line and it worked perfectly. Then I tried to use Yara rules inside my python app so I installed yara-python and used it in this way:
import yara
dex_path = './classes.dex'
my_rule = './rule.yar'
json_data = load_json_data()
rule = yara.compile(my_rule)
matches = rule.match(filepath=dex_path, modules_data={'androguard': json_data})
print(matches)
The match function works good when using Yara rules without import "androguard" module but when I want to apply a rule which imports androguard, the match function gives an error :
yara.Error: could not map file "./classes.dex" into memory
I'm applying a simple rule to an small file, in order of KB. I think that the problem is with the androguard module since when I remove the import "androguard", it works correctly. Any idea?
I had the same mistake with androguard, I solve the problem installing yara-python in the version 3.8.0
https://github.com/VirusTotal/yara-python/releases/tag/v3.8.0
I'm attempting to run an R script from python. I can see it's executing, but python is erroring out on a syntax error for the R script. The R script runs fine in RStudio, so I'm not sure what the issue might be. Any suggestions would be helpful.
This is my python code:
import sys
import subprocess
# other stuff going on
# execute R script which returns a document in a directory
subprocess.call([sys.executable, "C:/Users/path/sentiment_extraction.r"])
sentiment = pd.read_csv("new_sentiment_data.csv")
And here is my R script:
#! /usr/bin/Rscript
require(syuzhet)
today_news <- read.csv("C:/Users/path/Today_News.csv") # created from above python script
#Sentiments words table:
records <- as.character(today_news$News)
sentiment <- get_nrc_sentiment(records)
# other stuff that makes a file
Then this is the Python error I get:
File "C:/Users/path/sentiment_extraction.r", line 7
records <- as.character(today_news$News)
^
SyntaxError: invalid syntax
The data I use syuzhet on needs to be a character vector so I cast the column.
I figured it out. My parameters to run the R file were incorrect. I had to pass the path to Rscript.exe:
subprocess.call(["C:/Users/seeme/Documents/R/R-3.4.0/bin/Rscript",
"C:/Users/path/sentiment_extraction.r"])