I am using macOS. If I kill Terminal.app and then from a Python script I call
import subprocess
subprocess.Popen('open -a Terminal.app', shell=True)
Then the Terminal will open without conda working, that is, if I type conda in the programmatically opened terminal, then I get the error
Traceback (most recent call last):
File "/Users/home_folder/miniconda3/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
If I kill again the Terminal, then I open it the normal way, and then I close the only window but not killing the process (that is using CMD+W instead of CMD+Q), then if I run the script above, a new terminal window will open with conda properly working.
A similiar problem that I think is related to this is that if I open PyCharm analogously as above, then PyCharm is broken, I hope that a solution for the Terminal problem will be easily extended also to that case, otherwise I will create another question later.
(I am using virtualenv for this script, not sure it helps)
Related
action_publisher = subprocess.Popen(
["bash", "-c", "/opt/ros/melodic/bin/rostopic pub -r 20 /robot_operation std_msgs/String start"],
env={'ROS_MASTER_URI': 'http://10.42.0.49:11311\''})
I tried to run it shell=True and shell=False. Also calling it with bash or just running my executable and I am always getting an error:
Traceback (most recent call last):
File "/opt/ros/melodic/bin/rostopic", line 34, in <module>
import rostopic
ImportError: No module named rostopic
How can I make a call of a shell executable with open through python removing this issue? Tried all combination possible and also other stack proposed solution and still, it tries to import the executable instead of running it on a shell.
I can identify several problems with your attempt, but I'm not sure I have identified them all.
You should use subprocess.check_call or subprocess.run if you just want the subprocess to run, and your Python script to wait for that to complete. If you need to use raw subprocess.Popen(), there are several additional required steps of plumbing which you need to do yourself, which check_call or run will perform for you if you use these higher-level functions.
Your use of env will replace the variables in the environment. Copy the existing environment instead so you don't clobber useful settings like PYTHONPATH etc which may well be preventing the subprocess from finding the library it needs.
The shell invocation seems superfluous.
The stray escaped single quote at the end of 'http://10.42.0.49:11311\'' definitely looks wrong.
With that, try this code instead; but please follow up with better diagnostics if this does not yet solve your problem completely.
import subprocess
import os
# ...
env = os.environ.copy()
env['ROS_MASTER_URI'] = 'http://10.42.0.49:11311'
action_publisher = subprocess.run(
["/opt/ros/melodic/bin/rostopic", "pub", "-r", "20",
"/robot_operation", "std_msgs/String", "start"],
env=env, check=True)
If rostopic is actually a Python program, a better solution altogether might be to import it directly instead.
It sounds like the code of that file is trying to import the Python module. If you're getting that import error even when you try to execute the file in bash/from a shell, then it has nothing to do with subprocess.Popen.
From the traceback, it looks like it's a Python file itself and it's trying to import that module, which would explain why you see the issue when executing it from a shell.
Did you go through the installation correctly, specifically the environment setup? http://wiki.ros.org/melodic/Installation/Ubuntu#melodic.2FInstallation.2FDebEnvironment.Environment_setup
It sounds like you need to source a particular file to have the correct paths available where the Python module is located so it can be found when you execute the script.
As far as I can see, your .Popen command will try to execute
bash -c /opt/ros/melodic/bin/rostopic pub -r 20 /robot_operation std_msgs/String start
while bash -c has to be followed by a string. Thus, you may need to add single quotes.
Code in question:
from tkinter import *
root = Tk()
test_label = Label(root, text = "Hello")
test_label.pack()
root.mainloop()
I can only run this python code from the IDLE, if I run it any other way the window flashes on the screen and closes immediately. I've tried:
-adding an "input" line to keep it from closing
-running from the windows powershell
-compiling the code into an EXE with pyinstaller
and now I can't find any other suggestions. I tried making a simple 1-line program that just asks for input, and that works normally and stays open fine. The tkinter program works fine in IDLE but not any other circumstance.
What's happening here?
EDIT: If I run the program from the command line instead of windows 10 powershell, I get the following output:
Traceback (most recent call last):
File "C:\Users\Cam\Desktop\CSCI Notes\Programs\test.py", line 1, in
<module>
import tkinter
ImportError: No module named tkinter
However, the tkinter file is in the python library on my computer, and importing tkinter in python shell or IDLE works fine.
When I run python on command prompt, it shows python 2.7, so I changed tkinter to Tkinter and the program worked by importing the .py file
Using cmd:
Pyinstaller works(through canopy cmd prompt):
the first line is what I put into my IDLE's cmd prompt
Which populates the folder when executed:
And inside dist, I run test.exe which shows the window
NOTE: my system command prompt is using python 2.7 while the canopy(python environment) command prompt uses 3.5 with pyinstaller
Okay, I think I've solved this one! I read that windows can sometimes try to open .py files with python 2.7 instead, even if they're written in 3.6. I uninstalled python 2.7 from my computer, and now the file runs normally.
So for anybody having this problem, try to make sure your computer is opening python 3, not python 2.
I have also had this problem and as far as I can see no one has a proper solution.
The best I have found is putting -i before your filename. For example:
python -i yourfile.py
this will start the IDLE in the command line
This could also happen if you are using a modulo that isn't installed in your computer even if you have installed it using your IDLE. Try installing it with the command "pip install modulo" else try installing it manually.
Please consider the following problem:
I have a Python script that runs on a linux machine (Raspberry pi 3, running Rasbian Jessie) on boot.
This script has been added to sudo crontab -e
The script itself starts with no problem but is unable to load in a certain file that is in the same directory as the script (Desktop), I have any print statements/issues going into a log file. Which reads as follows:
Traceback (most recent call last):
File "/home/pi/Desktop/mainServ.py", line 18, in <module>
mouth_detector = dlib.simple_object_detector(mouth_detector_path)
RuntimeError: Unable to open mouthDetector.svm
I assume this is because the script has no access to a filesystem, or perhaps LXDE/Desktop at boot time? I could very well be wrong on this.
Any solutions to this problem will be greatly appreciated.
Whenever you execute a script via crontab be ready for environment variables to be different. In this case you can simply use the whole path in the file you are trying to reference.
To see what the current environment variables are from within Python, use:
import os
os.environ
You may find there are other differences between the crontab environment and whatever interpreter environment you are using for testing.
I have PyCharm Professional Edition 3.5 5.0 configured to
use IPython when possible
and in my Run/Debug Configurations I set
show interpreter afterwards
I use the interactive Interpreter a lot and I really like IPython, but there are some things that I don't like about the way this is handled in PyCharm:
any input() in my programs return empty strings.
Additionally, when an error occurs I can't interact with the Program anymore. (you can when you run a Python program with the -i flag)
There is a lot of space between the last line in the Console and the current line
In IPython the ...: prompt in a code block isn't indented 2 spaces and therefore not aligned to the In [?]: prompt.
When an error occurs I get something like this:
Traceback (most recent call last):
File "C:\Program Files (x86)\PyCharm\helpers\pydev\pydev_run_in_console.py", line 69, in <module>
globals = run_file(file, None, None)
File "C:\Program Files (x86)\PyCharm\helpers\pydev\pydev_run_in_console.py", line 29, in run_file
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Program Files (x86)\PyCharm\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/ca/Python/Bundeswettbewerb Informatik/Aufgabe2/Ameisen.py", line 133, in <module>
function_that_caused_error()
I don't need/want to see the traceback from the internals.
When running a file IPython needs to be started which takes some seconds even if I'm not going use the interpreted afterwards. I would like PyCharm to start IPython after the program has ended or when I start debugging (you can start IPython in an interactive console by doing import IPython; IPython.start_ipython()
There are some other minor things that I don't like:
When IPython is started it prints a lot of text to the console. I don't want to see any of it except maybe the version number (you can usually do this with the --no-banner option, but adding it to the interpreter options doesn't work)
when you type something and press Up it replaces what I have written with the last item of my history instead of replacing it with the last item of my history that start with what I have typed. Plain IPython does this.
I would like to have automatic code completion without having to press Ctrl + Space in the console
The "problems" are ordered by importance. Does anybody know how to change some of them? I could stop using IPython which would solve the second , the third and the fourth problem, but the other ones would still persist. All of this behavior (excluding the IPython stuff) is implemented very well in PyScripter.
EDIT:
I have found solutions to the first two problems and the problem with the IPython banner. The source for the PyDev interactive interpreter (which is used by PyCharm) is located, on Windows, in C:\Program Files (x86)\PyCharm\helpers\PyDev (path my vary of course).
So the first problem can be solved by editing the file _pydev_imps/_pydev_execfile.py. Wrap line 18 (exec(compile(contents+"\n", file, 'exec'), glob, loc)) in a try ... except block with the following code as the exception handler import traceback; traceback.print_exc(). This will terminate your Python program if there is an error while letting you interact with the variable afterwards.
Problem 2 can be solved by editing the fire pydev_run_in_console.py. Add this import at the beginning of the file: from pydev_console_utils import StdIn and insert sys.stdin = StdIn(interpreter, host, client_port) after what was line 61 before adding the import.
In order to solve the problem with the banner you have to download the most recent version of the PyDev source here and replace the files pydev_ipython_console and pydev_ipython_console_011 by their newer versions. In the newer version of the first file the __init__ method in line 22 has a argument called show_banner with the default value True. Change this to False.
This is probably not the answer you are searching for, but based on my experience using IPython on InteliJ products ( PyCharm, Ultimate), I don't recommend using their version of IPython. It's full of bugs, outdated and you'll lose precious time fixing problems instead of coding.
Have you tried jupyter notebook? If you installed python with anaconda, it's already installed. To run it, open the terminal and type:
jupyter notebook
If your browser doesn't open automagically, head to http://localhost:8888
Note:
You can automate this process by creating bat or sh script containing the code above inside your project directory, this way it doesn't start on you home dir, which it does by default.
Resources:
How to Install and Use IPython
nbopen project: Open a Jupyter notebook in the best available server
In all honesty, have you simply tried running a newer version of PyCharm and importing your settings? 3.5 is fairly outdated (I'm on 4.5.3 myself, newest version is 5.0) and I believe more support is offered for IPython in the newer versions. https://www.jetbrains.com/pycharm/help/ipython.html. Especially if you are a student, it might not hurt to give it a shot. I know older versions of PyCharm were more buggy than recent releases.
I am having trouble getting PYTHONPATH set. I have added this line to ~/.bashrc
export PYTHONPATH=$PYTHONPATH:/home/sayth/My_Programs/Python
I ran source ~/.bashrc
Here is the issue though if I type python at the terminal and get a python REPL I can then import modules from ~/My_Programs/Python without a problem.
However if I run bpython or idle or a console in ninja-ide etc then import fails.
>>> import temperature
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import temperature
ImportError: No module named temperature
>>>
I can do sys.path.append(/home/sayth/My_Programs/Python) and get it working, but I wanted the PYTHONPATH to work in all consoles automatically. How?
PS using ubuntu 11.10
I'm going to guess that you did not launch your ninja-ide from the same terminal that you source ~/.bashrc from -- the new environment variable only influences child processes of whichever shell process did the sourcing. Of course, logging out and in again may do it, but if you're living entirely in the GUI (e.g., you click a button to start the ninja-ide), then you may need to go to some more effort to set your environment variable. How you do so depends upon which graphical environment you're using.