I was working on a python script that I want to run via the task manager. I created a batch file like this:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
"C:\ProgramData\Anaconda3\python.exe" "\\insert_path_here\project_directory.py"
pause
When I have the project_directory.py as a path on my hard drive, it works fine with either the task manager or manually opening the batch file. When I put the .py file on a remote drive, the batch file still executes correctly when I manually open it. When the task manager tries runs the .bat file, I get a message asking which application I want to use to run the program:
What do?
Thanks!
Try using cmd.exe. on default you should find it here: c:\windows\system32\cmd.exe.
On the side note explore option of converting .py to .exe, you can start here: https://www.simplifiedpython.net/convert-python-to-exe-tutorial/
I feel like it would be better option for you.
It looks like it is trying to run the python.exe and then the project_directory.py file separately. Try putting those in as one command:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
"C:\ProgramData\Anaconda3\python.exe \insert_path_here\project_directory.py"
You probably have *.py files already associated with Python on your local machine which is why it works there.
Related
A little bit of context:
I have made a password manager in python
I then packed it into a single executable using:
pyinstaller --onefile password_manager.py
Okay first I want to specify that the script works properly when I run it through the password_manager.py file, the executable version also runs properly and when I e.g. create a user and I save the data in a new json file. I'm able to access it on runtime but when I actually close the shell and the program stops running it's like the data has all been lost and or deleted. I'm looking for a way for the required data to remain on my drive like they do when I run password_manager.py on its own.
P.S.
I can view hidden files in my computer too.
P.S.S.
The link to the code is here: code
My file structure when I run it through password_manager.py
Here is the executable showing some random Data I recently added without closing and restarting the program. These information was supposed to be stored in /data/users.json and /data/password (and is stored there when I just use password_manager.py) but when I actually exit the application they are not present in any way when I rerun the executable(neither the directories that were supposed to be created nor the files).
Simple replication:
#mkdir.py
import os
os.mkdir("stack")
shell:
pyinstaller --onefile mkdir.py
I also sadly couldn't find a way to include the executable in this question but you can replicate it with the given command.
I've got a Python script for retrieving PDF documents from a remote system and storing them locally. I run it manually from a Linux command line (using Ubuntu).
Whenever I store the file locally I would like to somehow add it to Recent Files so that it shows in the file dialog without me having to navigate through the directory structure to it.
How is this done? Is it some dbus magic? I don't need a complete solution, just a pointer where to look because I have no idea...
Thanks!
I currently have a Python script that scrapes some data from the internet, then saves it as a pickle file. When running this from a terminal with python filename.py it saves correctly, since the Date Modified field of the pickle file changes. However, when running with the built-in scheduler it doesn't actually save the pickle file, since the Date Modified isn't changed, despite seeing the Python script being executed (terminal opens up and I see the script running).
I ticked the Run with highest privileges box in the scheduler, and despite that it doesn't save the pickle file. I thought it had to do with it not having write permission, but if it has the highest priviliges, surely it can save a file?
At the scheduled time a terminal opens, so I know it is actually being executed (print a message to make sure), but it doesn't show an error about the fact that it couldn't save the file or anything like that. The only reason I know it's not working is the Date Modified field not changing. How can I fix this?
Windows Task Scheduler has a default working directory of C:\Windows\System32. If you set a relative path to the file you are trying to write, it will likely be written into that directory. If you open a Command Prompt in the directory of your script and run it, the relative path will be that directory. So, you actually have two copies of the pickle file.
If you set an absolute path in your script to the file you want to write to, both methods of running your script will write to the same file.
I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave
I would like a user on Windows to be able to run my Python program, so I want to convert it to a .bat file. Is there a way to convert it? I've tried searching, but didn't find anything.
Unless your script is trivial it will not be possible to 'translate' it into a batch file. However two options exist:
Create a batch file to run the python script
Attempt to compile the script into an executable
The first option is trivial. Simply create a batch file as so:
#ECHO OFF
PATH_TO_PYTHON\python.exe PATH_TO_SCRIPT.py
If you are in a corporate environment you could put a python installation on a network and create a batch file to run the script from there. Otherwise you will need the user to install python. If python is on their path then the batch file can be simplified to:
#ECHO OFF
python PATH_TO_SCRIPT.py
Alternatively, there are options available that attempt to compile scripts into .exe files. I've never had any success with them, but py2exe seems the most common.
No, I don't think you can reasonably expect to do this.
Batch files are executed by the Windows command interpreter, which is way way more primitive.
Python is a full-blown programming language with a rich and powerful library of standard modules for all sorts of tasks. All the Windows command interpreter can do is act like a broken shell.
On the other hand, Python is available on Windows, so just tell the user to install it and run your program directly.
you can do it in 2 ways :
create a normal text file with an extension .bat and write
#ECHO OFF
"python.exe location" "your file.py location"
create a normal text file with an extension .bat and write
"python.exe location" "your file.py location"
pause
(sorry for my English Im from armenia)
After 10 years I finaly did that without py2exe and some other things
I used openai playground to do that and thats worked:
I just writed "convert python code into .bat file: [my code]"
Just create a batch file that contains this two lines:
yourfilename.py
pause
Then run the batch file by double-clicking it.