I'm trying to download a zip file off the web and trying to download it by console command using wget -O fileName urlLink, but when trying the code, CMD opens for a second then closes and I canno't find the file anywhere.
I've tried using other ways of getting the file downloaded, but they return ERROR 403. Using wget in CMD downloads the right file, but not in the python code.
def gotoDownload(link):
try:
with requests.Session().get(link) as download:
if isUrlOnline(download):
soup = BeautifulSoup(download.content, 'html.parser')
filtered = soup.find_all('script')
zip_file_url = re.search(r"('http.*?')", filtered[17].text).group().replace("'", "")
os.system("wget -O {0} {1}".format('CreatureFinalZTL.zip', zip_file_url))
Expect the file to download
Instead doesn't download anything.
There are a few things that may help here (it may or may not solve your problem, because it is dependent on the your machine's setup and configuration). First, one thing I would suggest is to be more specific on the paths. You can use absolute paths in the wget line like so:
"wget -O {0} {1}".format('/path/to/output/dir/CreatureFinalZTL.zip', zip_file_url)
This is usually helpful in case the Python environment does not operate in a directory you are expecting. Alternatively, you can force the directory with the following python command:
os.chdir( path )
Then, you can operate with relative paths without worry. A second thing I would suggest is to confirm that the url is what you are expecting. Just print it out like so:
print( zip_file_url )
It might sound silly, but it is important to confirm that your regex is operating correctly.
Use subprocess instead.
import subprocess
...
subprocess.run(["wget", "-O", 'CreatureFinalZTL.zip', zip_file_url])
This avoids any shell involvement with the command you wish to run.
Fixed, had to re-add wget to PATHS on windows.
Related
Does anyone know what I am doing wrong with this command:
import subprocess
subprocess.call('tar -zvxf %s -C %s' % ("file.tar.gz", '/folder'), shell=True)
The code runs without any errors but the file is only unzipped on random occasions. I know I can use tarfile, but I would like to run it as a Linux command. Any ideas?
If you read the man page, you'll see that the -C parameter is sensitive to order -- it only affects operations that come after it on the command line. So, your file is being unzipped into whatever random directory you happen to be in.
You don't need shell for this. Do:
import os
import subprocess
os.chdir('/folder')
subprocess.call( ['tar','xvfz', 'file.tar.gz'] )
I have a Python package that I have uploaded to PyPP. The script calls two additional R scripts to run. I have verified that the required R scripts are also uploaded to PyPI (by physically downloading the latest version and seeing them present in the directory). I also can successfully install and run the main python script.
However, I am having trouble figuring out how to call the R scripts from within the Python script. That is, what directory structure do I use? Here is the command I use to run:
$ python_script -f file1.txt -g file2.txt
and I get this error:
Fatal error: cannot open file 'script.r': No such file or directory
In the Python script, here is how I am calling the R script:
cmd = [ 'Rscript', 'python_script/Rscript.r' ]
output = subprocess.Popen(cmd, stderr=subprocess.PIPE).communicate()
result = output[1].decode('utf-8')
But nothing I try works: I've tried just 'Rscript.r' and './Rscript.r'
I'm at a loss as to how to correctly call this script. It is in the same directory as the main python_script I am running.
The path here would be relative to where you're invoking python_script from, but your R scripts exist in a directory relative to where your package has been installed.
You can use __file__ to determine the full path to the file which is being executed. By splitting this, you can get a path to the directory where the package was installed, and then add any additional directories/filenames to get a full path to your R script:
import os
this_dir, this_filename = os.path.split(__file__)
RSCRIPT_PATH = os.path.join(this_dir, "Rscript.r")
cmd = ['Rscript', RSCRIPT_PATH]
output = subprocess.Popen(cmd, stderr=subprocess.PIPE).communicate()
result = output[1].decode('utf-8')
Note: Best practice to ensure cross-platform compatibility here is to use os.path.join('path', 'to', 'file.txt') to generate a path instead of path/to/file.txt, since not all platforms use / as a path separator.
I connected yesterday using the SSH protocol to another computer and tried to load, through Python, a SO file (which would be compiled C). Here is what I got in the CLI:
The file that is being requested (libLMR_Demodulator.so) next to "OSError:" is in the same dir as the file I want to load (libDemodulatorJNI_lmr.so).
The python code (v3.5.2) is the following one:
import ctypes
sh_obj = ctypes.cdll.LoadLibrary('./libLMR_Demodulator.so')
actual_start_frequency = sh_obj.getActualStartFrequency(ctypes.c_long(0))
print('The Current Actual Frequency Is: ' + str(actual_start_frequency))
#Charles Duffy is right. The issue come from dependencies. You can verify this by command:
ldd libLMR_Demodulator.so
You have several ways to fix this issue:
Put all the lib to /lib, /usr/lib paths, or directly install them to your system.
Put the libs' path to /etc/ld.so.conf file, then run ldconfig to refresh cache.
use LD_LIBRARY_PATH to add the libs' path, then try to run you script
LD_LIBRARY_PATH=[..path] python [script.py]
or
export LD_LIBRARY_PATH=[..path]
python [script.py]
You can check with manual of dlopen to get more details.
I got here looking for how to ensure that a module / package with a .so file was able to load another .so file that it depends upon -- changing the current directory to the location of the first .so file (i.e., in the directory where the module is) seems to work for me:
import os,sys,inspect
cwd = os.getcwd()
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
os.chdir(currentdir)
import _myotherlib
os.chdir(cwd) # go back
might also work for the OP case?
I am trying to run this tool within a lambda function: https://github.com/nicolas-f/7DTD-leaflet
The tool depends on Pillow which depends on imaging libraries not available in the AWS lambda container. To try and get round this I've ran pyinstaller to create a binary that I can hopefully execute. This file is named map_reader and sits at the top level of the lambda zip package.
Below is the code I am using to try and run the tool:
command = 'chmod 755 map_reader'
args = shlex.split(command)
print subprocess.Popen(args)
command = './map_reader -g "{}" -t "{}"'.format('/tmp/mapFiles', '/tmp/tiles')
args = shlex.split(command)
print subprocess.Popen(args)
And here is the error, which occurs on the second subprocess.Popen call:
<subprocess.Popen object at 0x7f08fa100d10>
[Errno 13] Permission denied: OSError
How can I run this correctly?
You may have been misled into what the issue actually is.
I don't think that the first Popen ran successfully. I think that it just dumped a message in standard error and you're not seeing it. It's probably saying that
chmod: map_reader: No such file or directory
I suggest you can try either of these 2:
Extract the map_reader from the package into /tmp. Then reference it with /tmp/map_reader.
Do it as recommended by Tim Wagner, General Manager of AWS Lambda who said the following in the article Running Arbitrary Executables in AWS Lambda:
Including your own executables is easy; just package them in the ZIP file you upload, and then reference them (including the relative path within the ZIP file you created) when you call them from Node.js or from other processes that you’ve previously started. Ensure that you include the following at the start of your function code:
process.env[‘PATH’] = process.env[‘PATH’] + ‘:’ + process.env[‘LAMBDA_TASK_ROOT’]
The above code is for Node JS but for Python, it's like the following
import os
os.environ['PATH']
That should make the command command = './map_reader <arguments> work.
If they still don't work, you may also consider running chmod 755 map_reader before creating the package and uploading it (as suggested in this other question).
I know I'm a bit late for this but if you want a more generic way of doing this (for instance if you have a lot of binaries and might not use them all), this how I do it, provided you put all your binaries in a bin folder next to your py file, and all the libraries in a lib folder :
import shutil
import time
import os
import subprocess
LAMBDA_TASK_ROOT = os.environ.get('LAMBDA_TASK_ROOT', os.path.dirname(os.path.abspath(__file__)))
CURR_BIN_DIR = os.path.join(LAMBDA_TASK_ROOT, 'bin')
LIB_DIR = os.path.join(LAMBDA_TASK_ROOT, 'lib')
### In order to get permissions right, we have to copy them to /tmp
BIN_DIR = '/tmp/bin'
# This is necessary as we don't have permissions in /var/tasks/bin where the lambda function is running
def _init_bin(executable_name):
start = time.clock()
if not os.path.exists(BIN_DIR):
print("Creating bin folder")
os.makedirs(BIN_DIR)
print("Copying binaries for "+executable_name+" in /tmp/bin")
currfile = os.path.join(CURR_BIN_DIR, executable_name)
newfile = os.path.join(BIN_DIR, executable_name)
shutil.copy2(currfile, newfile)
print("Giving new binaries permissions for lambda")
os.chmod(newfile, 0775)
elapsed = (time.clock() - start)
print(executable_name+" ready in "+str(elapsed)+'s.')
# then if you're going to call a binary in a cmd, for instance pdftotext :
_init_bin('pdftotext')
cmdline = [os.path.join(BIN_DIR, 'pdftotext'), '-nopgbrk', '/tmp/test.pdf']
subprocess.check_call(cmdline, shell=False, stderr=subprocess.STDOUT)
There were two issues here. First, as per Jeshan's answer, I had to move the binary to /tmp before I could properly access it.
The other issue was that I'd ran pyinstaller on ubuntu, creating a single file. I saw elsewhere some comments about being sure to compile on the same architecture as the lambda container runs. Therefore I ran pyinstaller on ec2 based on the Amazon Linux AMI. The output was multiple .os files, which when moved to tmp, worked as expected.
copyfile('/var/task/yourbinary', '/tmp/yourbinary')
os.chmod('/tmp/yourbinary', 0555)
Moving the binary to /tmp and making it executable worked for me
There is no need to copy the files the /tmp. You can just use ld-linux to execute any file including those not marked executable.
So, for running a non-executable on AWS Lambda, you use the following command:
/lib64/ld-linux-x86-64.so.2 /opt/map_reader
P.S. It would make more sense to add the map_reader binary or any other static files in a Lambda Layer, thus the /opt folder.
Like the docs mention for Node.js, you need to update the $PATH, else you'll get command not found when trying to run the executables you added at the root of your Lambda package. In Node.js, that's:
process.env['PATH'] = process.env['PATH'] + ':' + process.env['LAMBDA_TASK_ROOT']
Now, the same thing in Python:
import os
# Make the path stored in $LAMBDA_TASK_ROOT available to $PATH, so that we
# can run the executables we added at the root of our package.
os.environ["PATH"] += os.pathsep + os.environ['LAMBDA_TASK_ROOT']
Tested OK with Python 3.8.
(As a bonus, here are some more env variables used by Lambda.)
I wish to write a python script that allows me to navigate and git pull multiple repositories. Basically the script should type the following on the command-line:
cd
cd ~/Desktop/Git_Repo
git pull Git_Repo
I am not sure if there is a python library already out there that can perform such a task.
Use subprocess, os, and shlex. This should work, although you might require some minor tweaking:
import subprocess
import shlex
import os
# relative dir seems to work for me, no /'s or ~'s in front though
dir = 'Desktop/Git_Repo'
# I did get fetch (but not pull) to work
cmd = shlex.split('git pull Git_Repo')
# you need to give it a path to find git, this lets you do that.
env = os.environ
subprocess.Popen(cmd, cwd=dir, env=env)
Also, you'll need your login preconfigured.