I am trying to run the following bit of code as part of a larger python program to get my mac address:
if sys.platform == 'win32':
for line in os.popen("ipconfig /all"):
if line.lstrip().startswith('Physical Address'):
maccode = line.split(':')[1].strip().replace('-', ':')
break
else:
for line in os.popen("/sbin/ifconfig"):
if line.find('Ether') > -1:
maccode = line.split()[4]
break
When I run this code in python 3.5 as part of a .py program it works fine. When I compile my program using pyinstaller into an exe and I get to the following line it fails
for line in os.popen("ipconfig /all"):
Any ideas on how I can get around this problem? I have done some reading and I think I could use "subprocess" but I have been unsuccessful in creating the alternative line. Any help would be great.
Related
I've been working through the book 'Automate the Boring Stuff With Python' and am doing the mclip chapter project, the code is fine, but I'm having issues with the execution.
the idea of the program is that it's a callable program from window's Run function, you should be able to type in run: mclip [keyphrase] which corresponds to a dictionary in the program that then copies to your computer's clipboard, a message that you can easily paste.
the issue arises that when I'm running the program through PyCharm, it runs fine, but I can't execute with a sys.args variable to fully run the program, and when I try and run it using windows run, and I include a sys.args variable keyphrase, the program returns an error saying I don't have the ability to import pyperclip when I have done that before already
I have edited the PATH variable of my machine to include everything python related in at least three different configurations, but I don't know why the windows run application cannot find pyperclip, as every time I go to install pyperclip with "pip install pyperclip" at all locations python could be, and all locations that are being referenced in PATH, I get a "you already have pyperclip installed" message
I don't know what to do, and there's not any questions that I've seen thus far that have been helpful.
#! python3
# mclip.py - A multi-clipboard program
import sys
import pyperclip
TEXT = {'agree': """Yes, I agree. That sounds fine to me.""",
'busy': """Sorry, can we do this later this week or next
week?""",
'upsell': """would you consider making this a monthly
donation?"""}
if len(sys.argv) < 2:
print('Usage: python mclip.py [keyphrase] - copy phrase text')
sys.exit()
keyphrase = sys.argv[1] # first command line arg is the keyphrase
if keyphrase in TEXT:
pyperclip.copy(TEXT[keyphrase])
print('Text for ' + keyphrase + ' copied to clipboard.')
else:
print('There is no text for ' + keyphrase)
Found out to import pyperclip or other libraries through PyCharm's terminal tab, this imports the library directly to the virtualenv that Pycharm is running as its interpreter
I've recently started learning python and am still a newbie.
How can I determine if my code run from IDE or run standalone?
My code is not imported so
__name__ == "__main__" .
Someone suggested checking sys.executable name but I'm looking for a solution independent of the file name.
P.S: I create a standalone file with pyinstaller.
Here's a link to the page pyinstaller has on their site giving information about how to check the Run-Time information of your file: https://pyinstaller.org/en/stable/runtime-information.html
Basically it says to add the following code
import sys
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
print('running in a PyInstaller bundle')
else:
print('running in a normal Python process')
This should print the 'running in a PyInstaller bundle' message if it's all self contained and properly bundled, and it should print 'running in a normal Python process' if it's still running from your source code.
I want to convert the following sample python program into an executable file:
import os
print(os.getcwd())
To convert it into an executable I have used Pyinstaller:
pyinstaller app.py --onefile
And the EXE file is getting generated in the dist folder, but when I run it, it launches and immediately closes, and the expected print statement is not even displayed.
What could be the issue?
It does prints the statement, but just after printing it the code ends. You can add input() or use time.sleep(seconds) to make your program wait until you press a key or any particular number of seconds respectivly.
To check if your code(without the advice i have given) prints, start that python file in your command line.
Background:
I recently installed curses with pip install curses
I found a couple tutorials online (https://www.devdungeon.com/content/curses-programming-python, https://docs.python.org/3/howto/curses.html, https://www.youtube.com/watch?v=rbasThWVb-c)
I always constantly testrun my code when I install a new package or learn something new
Whenever I run s = curses.initscr() in IDLE, I get this error message:
File "C:/Users/jacob/AppData/Local/Programs/Python/Python37-32/screentest.py", line 3, in <module>
s = curses.initscr()
File "C:\Users\jacob\AppData\Local\Programs\Python\Python37-32\lib\curses\__init__.py", line 30, in initscr
fd=_sys.__stdout__.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'
This is the message from PyCharm:
Redirection is not supported.
Process finished with exit code 1
And when I run a sample snippet from DevDungeon,
print("Preparing to initialize screen...")
screen = curses.initscr()
print("Screen initialized.")
screen.refresh()
curses.napms(2000)
curses.endwin()
print("Window ended.")
in command prompt with python booted, it just gives an uninteractable blank screen.
Is the thing happening in shell correct?
What the hell is going on?
How can I fix this?
please help thank you
sys.__stdout__ is the original sys.stdout for a python process. When you start python with pythonw.exe, which only exists on Windows, python initially executes stdout = __stdout__ = None. (I am not sure about what happens on *nix.) pythonw.exe is used to run python with a GUI UI without an associated text console. On Windows, the IDLE icons and Start menu entries run python with pythonw. Same with other GUI IDEs.
curses runs with text terminals or consoles. It assumes that sys.__stdout__ is such. It cannot work when sys.__stdout__ is None.
If you start IDLE from a command line terminal/console with python -m idlelib, sys.__stdout__ will be that terminal, and it will have a fileno(). What will happen after that I do not know. If your program uses curses, you are likely better off to run it in the text console.
I have written a chess program in Python 3.4.3 and I am running the Python 3 interpreter in the interactive mode as follows:
python3 -i chess.py
However, the code after the class definitions get invoked twice and I do not know why. My code is on pastebin
You should remove the line from chess import * that is at the end of the file, it should not be needed.
Also, it is common to make sure that some of the code is not executed unless the code in the module is executed as a script.
if __name__ == '__main__':
# Not executed if the module is imported
g = Game()