I'm writing a program that I would like to make a stand alone for my company. It works perfectly when I run it from the sublime text shell and I have everything set up to go except one issue that I can't seem to solve; file paths that involve usernames. Does anyone have any suggestion on how to handle this?
An example is
wb.save(r'C:\Users******\Desktop\Excel.xlsx')
I want to make the ****** part either be automatic or an input box.
Use os.path.expanduser() with '~' where you want the home directory:
import os
print(os.path.expanduser('~/Desktop/Excel.xlsx'))
Alternatively use pathlib.Path:
from pathlib import Path
print(Path.home() / 'Desktop' / 'Excel.xlsx')
os.getlogin() will do
import os
path = os.path.join(r'C:\Users',os.getlogin(),'Desktop','Excel.xlsx')
print(path)
Awesome! Looks like that worked but it presented another error now when I create it as a stand alone.
Wait originally works when I run it from the shell using this code, where EC is expected conditions:
wait.until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_name('AppBody')))
Whenever I run it as a stand alone though I get the following error:
Traceback (most recent call last):
File "Stand_Alone_CAS_Automation", line 57, in <module>
NameError: name 'wait' is not defined
[17344] Failed to execute script Stand_Alone_CAS_Automation
Related
I've got a very simple python program I wrote to learn pygame, and among other things I use an image.
When I run the program with PyCharm, or when I run it by double-clicking on the file, it works fine. However, if I try to run it through the command prompt, I get the following error:
C:\Users\julix>C:\Users\julix\Documents\test\pygame_tutorial.py
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\julix\Documents\test\pygame_tutorial.py", line 21, in <module>
carImg = pygame.image.load("racecar.png")
pygame.error: Couldn't open racecar.png
This is the line in my code it refers to:
carImg = pygame.image.load("racecar.png")
The image "racecar.png" is located in exactly the same directory as the program.
The confusing part is that my code seems to be fine since there are no errors when I run it by double-clicking.
Can post full code if necessary.
Thanks in advance
The fact, that the file is in the same directory as the program doesn't matter. If you don't provide a path the program will look for the file in the working directory which might be a total different one.
If you want to use a specific directory add your path to the filename. A flexible approach would be to determine the path of the current file and use that. Python has a way to do that with os.path.dirname.
import os.path
print(os.path.dirname(__file__))
In this case it would lead to the following code:
import os.path
filepath = os.path.dirname(__file__)
carImg = pygame.image.load(os.path.join(filepath, "racecar.png"))
Here is an alternative implementation using the wonderful pathlib:
import pathlib
filepath = pathlib.Path(__file__).parent
carImg = pygame.image.load(filepath / "racecar.png")
I would suggest an easy solution, I got stuck at the same stage.
I was coding in VS Code and later realized that the terminal is currently executing code from parent directory.
So to import image I just need to get the terminal into my current working directory and then run the program again.
Images were load fine without any error.
Just make sure in the terminal you are executing code from the same working directory as the project folder.
I've same problem. but for me, it's wrong about file name.
*sub_img = os.path.join(img_folder, 'submarine.png.png')
self.image = pygame.image.load(sub_img).convert()*
I was edit my file name " submarine.png " after downloaded.
So, This file is name submarine.png type png.
You can fix it by change file name to just "submarine"
I hope it's helpful.
I wanted to use this library to scrape data from otodom. I've read the docs however I'm stuck on a very basic level, trying to do basic import. The code I use is taken directly from the docs. On top of that I've tried to launch example.py from Github but I'm getting the same error.
Here's the excerpt from the code:
import otodom, os, logging
from otodom.category import get_category
from otodom.offer import get_offer_information
Error message I get:
Traceback (most recent call last):
File ".\otodom_import.py", line 2, in
from otodom.category import get_category
File "C:\Users\Dom\Anaconda3\lib\site-packages\otodom\category.py", line 9, in
from otodom.utils import get_response_for_url, get_url
File "C:\Users\Dom\Anaconda3\lib\site-packages\otodom\utils.py", line 14, in
from scrapper_helpers.utils import caching, normalize_text, key_sha1,
get_random_user_agent
File "C:\Users\Dom\Anaconda3\lib\site-packages\scrapper_helpers\utils.py",
line 22, in
MAX_FILENAME_LENGTH = subprocess.check_output("getconf NAME_MAX /",
shell=True).strip()
File "C:\Users\Dom\Anaconda3\lib\subprocess.py", line 336, in check_output
**kwargs).stdout
File "C:\Users\Dom\Anaconda3\lib\subprocess.py", line 418, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command 'getconf NAME_MAX /' returned non-zero
exit status 1.
Thanks in advance!
EDIT: Don't understand downvotes, I realize that this is a niche module but I did my homework. I've read the docs and tried to find help in other places. If there is a chance that as a noobie I'll find at least one person that can help me, why wouldn't I use this opportunity? Even info that module is buggy can help.
getconf, which the dependency scrapper_helpers module is trying to call, is a POSIX command not available on Windows.
You can track it down in C:\Users\Dom\Anaconda3\lib\site-packages\scrapper_helpers\utils.py and replace the line 22 with:
MAX_FILENAME_LENGTH = 255
But then the question is what other error might pop up given that the module obviously expects to run on a POSIX-compatible system - for example, it will attempt to use /var/tmp/scrapper-helpers/ as its temporary cache path which is also not available on Windows (but Python will interpret it as <DRIVE_LETTER>:\var\tmp\scrapper-helpers and attempt to create it).
Alternatively, looking at its code, you might get away with just declaring the following environment variables:
set MAX_FILENAME_LENGTH=255
set CACHE_DIR=%TEMP%\scapper-helpers\
After following the instructions given on this site: https://sourceware.org/gdb/wiki/STLSupport
GDB is still unable to print the contents of stl containers like vectors, other than printing out a huge amount of useless information. When GDB loads, I also get the following errors, which I think are related to the Python that I put into ~/.gdbinit
Traceback (most recent call last):
File "<string>", line 4, in <module>
File "/Users/mayankp/gdb_printers/python/libstdcxx/v6/printers.py", line 1247, in register_libstdcxx_printers
gdb.printing.register_pretty_printer(obj, libstdcxx_printer)
File "/usr/local/share/gdb/python/gdb/printing.py", line 146, in register_pretty_printer
printer.name)
RuntimeError: pretty-printer already registered: libstdc++-v6
/Users/mayankp/.gdbinit:6: Error in sourced command file:
Error while executing Python code.
When GDB loads, I also get the following errors...
It looks like instructions you followed on https://sourceware.org/gdb/wiki/STLSupport are invalid now. If you look at svn log you will see that registering of pretty printers was added in __init__.py recently:
------------------------------------------------------------------------
r215726 | redi | 2014-09-30 18:33:27 +0300 (Вт., 30 сент. 2014) | 4 lines
2014-09-30 Siva Chandra Reddy <sivachandra#google.com>
* python/hook.in: Only import libstdcxx.v6.
* python/libstdcxx/v6/__init__.py: Load printers and xmethods.
------------------------------------------------------------------------
And therefore second registration throws error. You can remove it or comment out:
#register_libstdcxx_printers (None)
GDB is still unable to print the contents of stl containers
You have probably mismatched pretty printers with your gcc. See https://stackoverflow.com/a/9108404/72178 for details.
From your traceback it seems that the register_libstdcxx_printers() call is failing because there already is such a pretty printer registered. To avoid that, you can wrap it in a try..except to make sure instructions in .gdbinit don't interfere with the launch of GDB if they fail:
python
import sys
sys.path.insert(0, '/home/user/gdb_printers/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
try:
register_libstdcxx_printers(None)
except:
pass
end
(Note: You should usually never use a bare except statement without qualifying the type of exceptions you want to catch. This is a special case though, in startup configuration files like .gdbinit, .pdbrc or your PYTHONSTARTUP file you'll probably want to write defensive code like that).
But chances are this will only get rid of the ugly traceback for you, and printing of STL vectors still wont work. Because it seems there is already a pretty printer registered from somewhere else.
Make sure the path /home/user/gdb_printers/python actually matches the path where you checked out the module mentioned in the STLSupport docs.
I am returning strings from a python module, for consumption within a bash script.
This is what I have so far:
SCRIPT_DIR=/somepath/to/scripts/folder
cd $SCRIPT_DIR/eod
python_cmd='import sys;
sys.path.append("/somepath/to/scripts/folder/utils");
import settings as st;
print " ".join(st.foo())'
# Fix indentation for Python
python_cmd=${python_cmd//' '/''}
my_array=(`python -c "$python_cmd"`)
I want to use the DRY philosophy in the snippet above. However, the string/somepath/to/scripts/folder is repeated in the script. I would like to pass the definition of $SCRIPT_DIR into the python_cmd - however I tried (replacing the path string in python_cmd with $SCRIPT_DIR/utils and it failed with the following error:
Traceback (most recent call last):
File "<string>", line 3, in <module>
ImportError: No module named settings
What am I doing wrong?
Note:
I am running bash 4.1.5
Don't bother to pass the string in at all. Instead of modifying sys.path in the Python text, modify the PYTHONPATH environment variable in the bash script. It's easier, and is the preferred way to affect the import path anyway.
An example of how to set the path:
SCRIPT_DIR=/somepath/to/scripts/folder
cd $SCRIPT_DIR/eod
export PYTHONPATH=$SCRIPT_DIR
python_cmd='import settings as st;
print " ".join(st.foo())'
I also have to say, this seems like an odd way to glue components together, but I don't have enough information to recommend a better way.
Because the python command is enclosed in single quotes, $SCRIPT_DIR is not expanded. Try this:
python_cmd='import sys;
sys.path.append("'$SCRIPT_DIR'");
import settings as st;
print " ".join(st.foo())'
That said, I would go with the answer that modifies PYTHONPATH.
I am trying to run this python script called fselect in windows 7. It can be downloaded from this website: http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/ under this name called Feature selection tool. I am running it on Python 2.7.2.Facing a bit of problem running it..
Typed this first in IDLE:
>>> import pprint
>>> import sys
>>> print pprint.pprint(sys.path)
>>> sys.path.append("C:\Users\HP\Documents\MATLAB\libsvm-3.11\tools")
>>> import fselect
Usage: training_file [testing_file]
Then the problem is when i type the next part:
Tried this:
>>> ./fselect.py TrainVec
SyntaxError: invalid syntax
Next tried this:
>>> fselect.py TrainVec
SyntaxError: invalid syntax
Next tried this:
>>> TrainVec
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
TrainVec
NameError: name 'TrainVec' is not defined
Tried this also:
>>> TrainVec.mat
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
TrainVec.mat
NameError: name 'TrainVec' is not defined
What is the correct way of typing it? Need some guidance on it...
tried running using cmd but there is an error...
If you are trying to run the fselect.py directly from the command prompt, make sure that python is set into the path variable. For guidance with that, please read http://people.cis.ksu.edu/~schmidt/200f07/setpath.html.
The script will also invoke grid.py. grid.py requires gnuplot to be there. So ensure that grid.py is running properly and if necessary check the paths of the svm_train, svm_test in the script along with that of grid.py.
Hope it will work now.
If it is a tool, you should run it, not import it. And of course you should not try to enter random commands, even if they are valid shell commands, in your Python prompt.
Assuming TrainVec is your data (since you use it in the context of TrainVec.mat it must be a Matlab data file) then run it on the Command Prompt like this:
python fselect.py TrainVec.mat
The example of ./fselect.py is intended for Unix systems. Make sure you run the above command in what ever directory you have saved fselect.py in.
If you need to write your own scripts to leverage this .py file then I refer you here for an example of how to do this.
Like the previous answer said, it looks like you are (incorrectly) trying to run the script from inside the Python interpreter. According to the documentation on the page you link to, it is not a module but a free-standing script and should be run as such:
Usage: ./fselect.py training_file [testing_file]