I'm using Python inside Pycharm, and wanting to scheduling code to run to open a workbook and save every day at a specified time. The function I've written to open and save the workbook has been working properly, but I'm now running in to two issues.
I cannot Import time, it's greying out and I'm not sure why
I'm receiving error "AttributeError: 'function' object has no attribute 'day'", and I'm not sure why. error occurs at ".day" within schedule function
import schedule
import time
def Date_Table():
from openpyxl import load_workbook
wb = load_workbook(r'file path')
wb.save(r'file path')
schedule.every.day.at("1:00").do(Date_Table)
I reviewed the functions in the "schedule" package. You just need to correct some syntax. Instead of:
schedule.every.day.at("1:00").do(Date_Table)
you probably want:
schedule.every().day.at("01:00").do(Date_Table)
Note the parentheses in the function call.
Hope that helps.
Regards.
Related
I'm trying to use multiple files in my programing and I ran into a problem.
I have two files: main.py, nu.py
The main file is:
import numpy
import nu
def numarray():
numpy.array(some code goes here)
nu.createarray()
The nu file is:
def createarray():
numpy.array(some code goes here)
When I run main I get an error:
File "D:\python\nu.py", line 2, in createarray
numpy.array(some code goes here)
NameError: name 'numpy' is not defined
numpy is just an exaple, I'm using about six imports.
As far as I see it, I have to import all moduls on all files, but it creating a problem where certain modules can't be loaded twice, it just hang.
What I'm doing wrong and how can I properly import functions from another file while using imported modules from the main file?
I hope i explain it well.
thanks for helping!
I have years in python and importing from other files is still a headache..
The problmen here is that you are not importing numpy in "nu.py".
But as you say sometimes it's a little annoying have to import al libraries in all files.
The last thing is, How do you get the error a module cannot be imported twice? can you give me an example?
In each separate python script if you are using a module within you need to import it to access. So you will need to 'import numpy' in your nu.py script like below
If possible try keeping the use of a module within a script so you dont have import the same multiple times, although this wont always be appropriate
I need to import a function classify(par1, par2, par3)
from a module called _Y03_Labeling. Importing does work, but using its functions with more than one extra parameter doesnt work.
Question : How can I import functions with more than one Parameter?
.
What I already tried ( Without Success):
I successfully can run the whole notebook2 from notebook1 with the following code:
import _Y03_Labeling
Labeling =_Y03_Labeling
(Why I know if it is successful? Because its comments are printed out). Whenever I try to run:
X,y = classify(a,b,c)
I get the following error: "TypeError: 'module' object is not callable"
I tried many variations from the import line, including:
import _Y03_Labeling
Labeling =_Y03_Labeling
X,y = Labeling.classify(a,b,c)
# or:
from _Y03_Labeling import classify
# or:
import _Y03_Labeling
X,y = _Y03_Labeling .classify(a,b,c)
Sadly none of them worked for me.
Things i also did so far:
shutting down the _Y03_Labeling notebook before I run the main Notebook
putting the function in the second notebook into a class, importing the class from the notebook and calling the function. (works only if function needs 1 parameter)
also i didnt forgot "self" i the function declaration with the class try.
I am glad, this forum exists and thankful for every possible help.
I fetched a cristal structure of a protein using the function retrieve_pdb_file from Bio.PDB. The default format has changed from PDB to PDBx/mmCif. I want to extract the protein sequence from the header in the cif file. There is supposed to be a simple function in Bio.PDB called MMCIF2Dict to do this but the module is not callable. I also downloaded the cif file manually and put it in the script folder but still the same error. My biopython is up to date. Am I doing something wrong or the module is not well implemented? Thank you for your answers.
from Bio.PDB import *
cifFile = '1bu7.cif'
mmcif = MMCIF2Dict(cifFile)
TypeError: 'module' object is not callable
The module is well implemented. The problem with your code is that you are calling a module instead of a function. In your particular case the module and the function have the same names hence the confusion.
To solve that you need to fix your code as follows:
from Bio.PDB import *
cifFile = '1bu7.cif'
mmcif = MMCIF2Dict.MMCIF2Dict(cifFile)
Try:
from Bio.PDB.MMCIF2Dict import MMCIF2Dict
Instead of :
from Bio.PDB import *
It's been a while since I have used Python and am stumbling already at a simple import!
statements.py:
str = "hello"
main.py:
import statements
print statements.str
Obviously the final program will have more going on, and the statements will be stuff like URLs. For this simple example however with both files sitting side by side in the same folder I get the following error on running main.py:
AttributeError: 'module' object has no attribute 'str'
I know I am doing something terribly silly, but I can't see what it is. Searching around it looks fine. Any help/insight is appreciated.
You used the import statement correctly. The error then can be caused by either importing a different module with the same name, or by having altered the module after already having imported it.
You can check print statements.__file__ to see what module Python found instead when importing, and you can use the reload() function to ask Python to reload the module from disk if you altered it.
I'm trying to figure out xlwings. I have the following python code:
from xlwings import xlfunc
from datetime import timedelta, datetime
#xlfunc
def convert(gmt):
gmtValue = datetime(int(gmt[6:10]), int(gmt[3:5]), int(gmt[0:2]), int(gmt[11:13]), int(gmt[14:16]), int(gmt[17:19]))
localTime = gmtValue - timedelta(seconds=(6*3600))
return localTime
All it does is take a time stamp string such as 05/01/2016 14:25:56 GMT and pulls off the GMT and converts it into local time (central time for the US.) I'm trying to use that code along with:
Sub convertToLocal()
RunPython(“import converToLocal; convertToLocal.convert(gmt)”)
End Sub
To make a user defined function but Excel doesn't seem to recognize the python code even though I have the xlwings.bas module imported. The only error I get is “Compile error: Expected end of Statement.” Which I think just means Excel doesn't recognize the python code. Any idea how to fix it? Also, what is the correct way to pass the contents of a cell into the python code?
RunPython and the decorator syntax are two different approaches. To do user defined functions with decorators, follow the guide here. You ll need to import those function with the add-in, no RunPython needed.