I want to run a Python script "test.py" in my usr/local/bin directory, which will create a text file in the same directory and write some brief text to it.
This script is called by a Python subprocess in code that responds to a GET request to my web application, which resides at var/www
subprocess.Popen(["python", "usr/local/bin/test.py"])
The code runs without error, however the text file is not created. The code to to do this is straightforward:
myfile = open('test.txt','w+')
myfile.write("Write some text\n")
myfile.close()
I've tried passing options along with this including shell=True. As well, I've tried to use the interface provided by os.system(), also to no avail.
Just for testing purposes, I've given full permissions to all pertinent directories.
Running the test.py script via CLI works as intended, however not when called by the web application. What could be the issue?
There could be several reasons:
The current directory isn't what you expect. When you use subprocess.Popen() without specifying a current directory, the child inherits the current folder from the parent process. So it will run probably in the folder in which the web server was started.
Try an absolute path.
There is a / missing in your path: /usr/.... Without this, the script will again be search relative to the current directory.
You should really fix your error handling before you try to fix the other issues. You should have gotten a useful error message from the code above. If you didn't, then this is a very bad sign: It means that you won't notice if something breaks later.
At some time in the future, you will make a small change that will break something. Without good error handling, this will go unnoticed.
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 have a TKinter script that lets the user choose from a list of existing scripts, input the variables that particular script requires, and then passes the variables to that script to be run. When all files are stored locally it goes off without a hitch. Once I tried to move it onto the network, so that colleagues can use it, it stopped executing the script.
There is no error message, just nothing happens. For troubleshooting simplicity I wrote a simple script that just calls a second script that prints "success", and the same issue happens where the simple scripts work locally, but not when stored on the network.
Script1.py:
import os
path=r'G:/files/Script2.py'
os.system(path)
Script2.py:
print("success")
No error messages, and if I try os.path.exists(path) it returns "True".
os.system(path) returns no errors even when the path is known to be incorrect.
If I do try open(path) for a path that is actually incorrect I get: FileNotFoundError: [Errno 2] No such file or directory.
I also tried setting path equal to the global drive name instead of G, using the IP address, changing the direction of the slashes, putting in double slashes after G:, and not using r''. Everything gives me the same result.
I also tried using path=os.getcwd()+"//Script2.py" since currently the files are all in the same folder on the network, again no success.
I am currently accessing the network through a VPN from home.
From googling I came up with these, but couldn't get a solution from them either:
Accessing a network folder through a python program
Using Python, how can I access a shared folder on windows network?
use a python3 or python keyword before
os.system("sudo python3 {}".format(path))
or:
os.system(f"sudo python3 {path}")
So I wrote this script that scrapes inventory levels from rival companies on amazon. I wrote the script in pycharm and when I run it there it works fine. However, I would like to automate the searching process so that it automatically collects data a few times per day. I made a scheduled task in the windows task scheduler but it doesn't seem to do anything. I'm pretty sure the problem lies in the fact that I import functions from different files in the same directory. When I try to run the same script in the python IDLE it gives me an error that the module (which is my directory which holds the other python scripts) cannot be found (as stated before it runs fine in pycharm).
from Bolcom.inventoryscraper.Inventory_scraper import inventory_scraper
Bolcom being the main directory which holds both files.
I hope the problem is clear if so does anyone see what I can do to fix it?
thanks in advance!!
I run into the same problem.
I use:
import sys
sys.path.append("venv\Lib\site-packages\") to solve directory problem
it seems like Pycharm put all library in different directory from the py file.
if you create you want to import your own module, you can either put your module into site-packages or use
sys.path.remove("venv\Lib\site-packages\")
sys.path.append("your module directory")
hopefully helps
I am developing a web app that relies on NodeJS and the Express module in which I am trying to run a Python script from an express route. What I would like to do is store the Python script in /public/scripts/test.py and refer to it using a relative URL such as /scripts/test.py so that Node doesn't need to know anything about the environment in which it is running.
I've attempted to execute the Python script both by using the python-shell module and by simply using Node's built-in ChildProcess. In both cases I'm running into the same issue – the path to the Python script seems to treated as being absolute and thus the script isn't executed, resulting in a file not found error.
How can I go about invoking a Python script with a relative URL? I'm rather new to web development, so I would not be surprised if I was simply misunderstanding the situation.
EDIT:
As jfreind00 pointed out, process.cwd() can be used to identify the present working directory on top of which a URL to the script can be built. Worked like a charm.
Turning my comments into an answer so you can mark this question as resolved.
Invoking a program from node follows all the normally expected path rules. If you precede the path with a /, then the system looks in the root of the current directory volume. If you don't want node to know about your external directory structure, then set an environment variable for the location or path prefix for your python script and have the node script read that environment variable. Either way, node has to either know about the actual path, it has to be in the node current working directory or it has to be in the path and you have to run it with something that will search the path.
If you want to see what the current working directory is in your particular configuration, then you can use:
console.log(process.cwd());
to see what the current working directory is so you can see how to base your path on that.
Not a major issue but just an annoyance I've come upon while doing class work. I have my Notepad++ set up to run Python code straight from Notepad++ but I've noticed when trying to access files I have to use the full path to the file even given the source text file is in the same folder as the Python program being run.
However, when running my Python program through cmd I can just type in the specific file name sans the entire path.
Does anyone have a short answer as to why this might be or maybe how to reconfigure Notepad++?
Thanks in advance.
The problem is that your code is assuming that the current working directory is the same as the script directory. This is not true in general. Of course it is true if you're in a cmd window, and you cd to the script directory before running it.
If you don't want to rely on that (e.g., because you want to be able to run scripts from Notepad++, or directly from Explorer), what you want to do is use the script directory explicitly. For example:
import os
import sys
scriptdir = os.path.abspath(os.path.dirname(sys.argv[0]))
with open(os.path.join(scriptdir, 'myfile.txt')) as f:
# etc.
If you have a ton of files that your scripts reference in a ton of places, it might be better to explicitly set the working directory. Just add one line:
os.chdir(scriptdir)
For anything beyond quick&dirty scripts, it's usually better to build an installable package and use pkg_resources to access the data files. Read the Tutorial on Packaging and Distributing Projects for more details. But as long as you're only hacking up scripts to help you maintain your specific system, the scriptdir solution is workable.
In the properties of the shortcut that you use to start Notepad++, you can change its working directory, to whichever directory you're more accustomed to starting from in Python. You can also begin your python program with the appropriate os.chdir() command.