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
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 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.
I'm trying to execute a visual basic script that is located in my personal macro workbook on an excel file that I'm creating. Here's what I have so far:
import os
import win32com.client
df2.to_excel("Apartments.xlsx")
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.open(filename="C:\Users\my\full\path\Apartments.xlsx", ReadOnly=1)
xl.Application.Run("Apartments.xlsx!create_chart.create_chart_proc")
It's throwing an error when opening the excel file on line 5, I have a feeling line 6 won't work either because it comes from my personal macro book. Anyone have ideas on how to get it to function?
PS. my module name is "create_chart" and my macro name is "create_chart_proc"
It would help if you said what error you are getting, and I know that this path
"C:\Users\my\full\path\Apartments.xlsx"
is not really what is in your code. But \ inside a string has a special meaning in Python (for example \n is a newline and \f is a formfeed). Whether your \ is interpreted as you intend depends on the character that comes after it. When you use Windows paths in Python it's generally less error-prone to use a raw string.
r"C:\Users\my\full\path\Apartments.xlsx"
When you get that line to work, if the next line gives trouble, then post that as a separate question.
I got the first part of the code to work simply running the code as follows:
xl.workbooks.open("C:\Users\my\full\path\Apartments.xlsx")
Posted a separate question to get the macro to work correctly.
I ran across this problem when trying to run code found in this answer to a question about loading Salome from a Python script (Salome is a 3D modeling program). The part of the code relevant to my problem was in creating and re-opening a .txt file. When attempting to open the file, I was getting an error that said there was no such file/directory as that file.
Then I tried just using savetxt() for just some random numpy array (with the directory being my desktop, acheived using os.chdir()), and no file was saved to my desktop, as far as I could tell. Then, to test if the file had been created somewhere without me noticing, I tried using loadtxt() to find it, and I got the same error saying there was no file or directory named MyFile.txt.
Here's my code:
import os
import numpy as np
os.chdir('C:\\Users\\Brahm\\Desktop')
np.savetxt('stuff',npa([7,8]))
np.loadtxt('stuff.txt')
I also tried without quotation marks around stuff in the savetxt line
Is this a bug, or am I doing something incorrectly?
In your program, you are saving your array using -
np.savetxt('stuff',npa([7,8]))
The file name is 'stuff' , not 'stuff.txt' (Please note the difference). Then you are trying to load - np.loadtxt('stuff.txt') . This will not work, because you created file as - stuff , not stuff.txt .
Either store to stuff.txt using -
np.savetxt('stuff.txt',npa([7,8]))
Or load from stuff -
np.loadtxt('stuff')
Here's my first simple test program in Python. Without importing the os library the program runs fine... Leading me to believe there's something wrong with my import statement, however this is the only way i ever see them written. Why am I still getting a syntax error?
import os # <-- why does this line give me a syntax error?!?!?! <unicode error> -->
CalibrationData = r'C:/Users/user/Desktop/blah Redesign/Data/attempts at data gathering/CalibrationData.txt'
File = open(CalibrationData, 'w')
File.write('Test')
File.close()
My end goal is to write a simple program that will look through a directory and tabularize data from relevant .ini files within it.
Well, as MDurant pointed out... I pasted in some unprintable character - probably when i entered the URL.