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.
Related
I've recently heard that you can change the prompt in python by changing variable sys.ps1. So I've decided to open IDLE, and write something like that:
>>> import sys
>>> sys.ps1 = ":::"
However, that created a new variable and nothing changed (prompt was still ">>>") - I rebooted IDLE and checked is this variable read by python... Nope:
>>> sys.ps1
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
sys.ps1
AttributeError: module 'sys' has no attribute 'ps1'
So, how do I change the prompt in python?
NOTE: The other functions/variables of sys module were read properly.
See issue #13657, "IDLE doesn't recognize resetting sys.ps1", on the Python bug tracker. It was opened in 2011 and is still unresolved (as of this writing). So it is not possible to change the prompt from within IDLE's Python shell simply because IDLE does not support that.
Update (less than 24 hours later): The issue is now closed. In the upcoming version of IDLE, to be released with Python 3.10, prompts will be displayed differently. See comment by IDLE developer Terry Jan Reedy above. A setting to change the prompt may be added in the future.
In the current and earlier Python/IDLE releases, the prompt can be customized before starting IDLE, but not while running it. You'd need a little start-up script that does this:
import sys
sys.ps1 = '::: '
import idlelib.idle
The reason you get that error message (module 'sys' has no attribute 'ps1') is because the Python shell that IDLE presents to you is not actually in "interactive" mode. And only then is sys.ps1 defined. You'd see the same error message if you tried to access sys.ps1 in any other Python program that is directly executed. In this case, that Python program is IDLE itself.
The prompt strings specifying the primary and secondary prompt (their initial values in this case are >>> and ... ) of the interpreter are only defined if the interpreter is in interactive mode and IDLE is more or less an integrated development environment for Python
Python interactive mode:
sys.ps1 doc
I have the following, minimal test program.
import pygame
pygame.init()
print(pygame.display.list_modes())
If I try to run this program via a Tox command, I get the error:
Traceback (most recent call last):
File "test.py", line 3, in <module>
print(pygame.display.list_modes())
pygame.error: video system not initialized
However, if I activate the virtual environment the tox command runs in and run the same program manually, it works as expected. I would expect both execution environments to be the same. What am I doing wrong?
In order to fix the problem I needed to add a
passenv = DISPLAY
to my [testenv].
As mentioned in the comment above, tox does an isolated build, and does not pass in ENVIRONMENT variables, except you whitelist them.
I am not super familiar with pygame, I just had a quick look in the source.
This works on my machine with Ubuntu - maybe it is different for your setup.
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)
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.
Okay I am newer to python and have been researching this problem but I can't find anything like it so I am not sure what is going on.
I am creating a program that involves sage and it has a message cue. We have this set up on a development machine, so I know it works but I was wanting to set it up on my own computer so I could get a better understanding of how it all works and make it easier to develop for myself.
To start up sage, we run a script that calls sages main binary file and passes it an executable .py file. (./sage/sage ./sage_server.py) This creates an error in the sage_server.py file:
Traceback (most recent call last):
File "./sage_server.py", line 23, in <module>
from carrot.messaging import Publisher
ImportError: No module named carrot.messaging
But whenever I run that file just in the terminal (./sage_server) the import works fine and isn't until line 27 that there is an error when it tries to import something from sage.
Does anyone know what would cause the error when it is being called by something else? I am very lost as to what would be causing this.
Sage has its own python, separate from the system libraries. This "carrot" module, whatever it is, must be installed in whatever python ./sage_server.py uses, but not in Sage.
You should be able to use either
[your-sage] -sh
to start up a Sage shell and use easy_install, or you could get whatever carroty package you're using, find its setup.py file, and then run
[your-sage] -python setup.py install
where obviously your-sage is the path to your sage.
Things get a little trickier if the install process isn't setup.py-based.