Getting information about current hg repository - python

Got a problem using subprocess.Popen(..)
This command:
HG_REV = subprocess.Popen(["hg", "log", "-l", "1", "../"], stdout=subprocess.PIPE).communicate()[0]
gives me the following Error:
File "/usr/lib/python3.4/site.py", line 182
file=sys.stderr)
^
SyntaxError: invalid syntax
But this one works fine:
HG_REV = subprocess.Popen(['ls','-la'], stdout=subprocess.PIPE).communicate()[0]
Could anyone explain, why this error occurs and how to solve it?
Refered to this post: Mercurial scripting with python
================== UPDATE 1 ==============================================
I figured out it has something to do with pydev and anything in my eclipse is probably changing the environment variables. I am pretty sure because running the above commands in a file (python3.4 test.py) on linux shell worked!!
Here are more information:
Ubuntu 14.04.3 on virtual machine
Eclipse Mars
pydev
python3.4 is the current configuration (when I switch to python2.7
it worked)
(I also tried hglib and the exact "same Error" occurs, when running
client = hglib.open(my_mercurial_repo) because open is also using subprocess.Popen(..) )
================== UPDATE 2 ==============================================
When setting PYTHONPATH to /usr/bin/python3.4 the code works!! But the behavior seems weird to me. What is the common way for using python3.4 on virtual machine? The question is still not solved.

Related

Code runner for vscode can't run python without error

I've installed Python 3.10.5 on my Windows 10 machine. Works splendidly running a file by writing "python file.py". What I can't do though, is to get the extension code runner to run python files through the hotkey ctrl + b. I've reinstalled Python (made sure to check PATH). Rebooted the computer and reinstalled code runner.
If I change no settings I get this error:
"/usr/bin/python3: The term '/usr/bin/python3' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
I found this similar thread: Code Runner does not work for Python in VsCode
After changing the code in settings.json to
"code-runner.executorMap": {
"python": "\"$pythonPath\" $fullFileName",
}
I get the error:
1 | … Data\Local\Programs\Python\Python310\python.exe"
"c:\lekspel\test.py"
| ~~~~~~~~~~~~~~~~~~~~
| Unexpected token '"c:\lekspel\test.py"' in expression or statement.
adding in a "-u" returns this error:
Unexpected token '-u' in expression or statement.
Anyone got a clue how to sort this out?
I solved it by doing a fresh install of vscode, deleting my settings in roaming. Not an optimal solution by any means, but got it out of the way at least.
got the same error. it was because i added "py" to path during installation setup (check by typing "py" in terminal if it opens python) so coderunner wasn't able to run python with "python" keyword, anyway try the following solution and tell me if it works:
1.Go to extensions
2.Right click -> extension settings
3.Edit settings in Json for any option
settings of coderunner
4.change "python -u" to "py -u"
like this
5.save json file with crl+s.
Run code again
tell me if it works for you.
here mine worked like this

Python sleep do not work in a Conda Environment in VS Code

I have the following code snippet
import time
print("Printed immediately.")
time.sleep(2.4)
print("Printed after 2.4 seconds.")
In the VS Code: Conda Environment it does not print statement 01 first and statement 02 after 2.4s delay. Instead both are printed at the same time after 2.4s delay.This issue is not visible in the native python version (Python 3.x with Mac OS). How to resolve this issue?
p.s I found out that this error is related with the executing command. The default command which is executed is
conda run -n env_name python "path/test_print.py"
if it is changed as follows it prints with the time delay
python "path/test_print.py"
how to change the default executing command in VS Code?
I think the underlying issue here is that "conda run" buffers stdout until the command completes - see: https://github.com/conda/conda/issues/9412 - but in the mean time I guess the only workaround is to change the executing command. I think the default launch command changed in the most recent (2020.1.57204) VS Code python extension release as I didn't have any such problems before this.
I found that changing the VSCode Python extension setting for condaPath from the default (blank) to something invalid - eg condaX - has the effect of changing the launch command from "conda run -n ...", to "& C:/ProgramData/Anaconda3/envs/tf-gpu/python.exe full_path_to_py_file.py" - which has the effect of running in the appropriate conda environment (tf-gpu in my case) without the IO buffering issue. (I'm running on Windows obviously.)
I had previously set up conda launching using https://medium.com/#udiyosovzon/how-to-activate-conda-environment-in-vs-code-ce599497f20d and I'm not sure if any of those changes had any bearing on the issue but I have since reinstalled the VSCode python extension and can find no trace of those changes, so I doubt any of them are necessary for the condaPath change to work as Ive described.
I'm assuming you don't use any virtual environment in native python, you can do it by:
Open up a terminal in VS Code (or use OS' terminal)
Assuming your default python is linked to python2, type which python3. You will get something like /usr/bin/python3
Open VS Code's user settings in the project folder you in. If there is not, create one: .vscode/settings.json.
Type and save
{
"python.pythonPath": "/usr/bin/python3"
}
Or if you want to change it globally, do it in the settings file -> preferences -> settings and search for python path, and make it /usr/bin/python3

'emacs': unknown terminal type

I'm getting the following message in the PyCharm Console when selecting the debug button:
pydev debugger: process 13699 is connecting
Connected to pydev debugger (build 145.1504)
'emacs': unknown terminal type.
I'm running on Ubuntu and echo $TERM displays: xterm
I installed emacs, elpy and ipython and the next time I ran pyCharm I ended up with this issue as well.
I fixed it by editing:
/home/$user/.pycharm_helpers/pydev/_pydev_bundle/pydev_ipython_console.py (line 6 for me)
Before, giving error:
os.environ['TERM'] = 'emacs' #to use proper page_more() for paging
After, and no more error:
os.environ['TERM'] = 'xterm' #to use proper page_more() for paging
Hope this helps :)

Error Initializing Interactive Concole

A funny behavior started happening on my Win10 64 machine.
PyDev refuses to initialize a "Console for currently active editor" with the following error message:
Error initializing console. Unexpected error connecting to console.
Failed to recive suitable Hello response from pydevconsole. Last msg
received: Console already exited with value: 1 while waiting for an
answer.
Command Line used: C:\Anaconda\python.exe -u
D:\eclipse\plugins\org.python.pydev_4.5.4.201601292234\pysrc\pydevconsole.py
51249 51250 51249 51250
Environment: USERDOMAIN_ROAMINGPROFILE=... PATH=C:/Program
Files/Java/jre1.8.0_71/bin/server;C:/Program
Files/Java/jre1.8.0_71/bin;C:/Program
Files/Java/jre1.8.0_71/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program...
...
PYTHONPATH=D:\eclipse\plugins\org.python.pydev_4.5.4.201601292234\pysrc\pydev_sitecustomize;D:\CORRECT
PATH TO PYTHON PROJECT IS
HERE;C:\Anaconda\DLLs;C:\Anaconda\lib;C:\Anaconda\lib\lib-tk;C:\Anaconda;C:\Anaconda\lib\site-packages;C:\Anaconda\lib\site-packages\Sphinx-1.3.1-py2.7.egg;C:\Anaconda\lib\site-packages\cryptography-0.9.1-py2.7-win-amd64.egg;C:\Anaconda\lib\site-packages\win32;C:\Anaconda\lib\site-packages\win32\lib;C:\Anaconda\lib\site-packages\Pythonwin;C:\Anaconda\lib\site-packages\setuptools-17.1.1-py2.7.egg
...
I tried to put some additional commands for sys.path or os.environ + os.chdir into the settings for default commands at initialization, but it never seems to reach that phase and as you can see from the entry in bold the correct path to the python project is there anyway.
Not really sure what else I can provide as info here. It does seem to happen only for some of my projects now.
For other or for new ones it works just fine...
Working with Eclipse Mars 4.5.1 EE and if not obvious from the log PyDev 4.5.4.
I am at a loss... Any ideas?
Try:
pip uninstall traitlets
As is documented here
It's worked for me

VBoxManage not working from Python

I want to delete a given virtual machine installed on VirtualBox (itself installed on Windows XP). When I run the command from CMD the deletion rus as I expect. But when I run this code:
import subprocess
myVM="windowsxp_1"
status = subprocess.call(["VBoxManage", "unregistervm", myVM, "--delete"])
The deletion is performed. However, unlike in the first case, the name of the VM is still listed on the VirtualBox interface. Why and how can I resolve this problem ?
Here are 2 pictures explaining what I said:
Deletection is successful:
But the name of the VM is still listed on the VirtualBox:The result is the same when I use :
import os
os.system("VBoxManage unregistervm vmNameToDelete --delete")
I can't reproduce this problem using Python run from the command line. It appears your IDE may be affecting things. Try running your Python script from the command line to see it works there.
I'm not sure why running the script under your IDE that would cause it not to work. Maybe you've got two versions of VirtualBox installed on your machine and your IDE is using a different PATH and ends up using a different VBoxManage.

Categories