I have created a Python program which works great on windows, but now I have brought it over to my Apple Mac to test it out. The application has a sqlite database and I also have a couple of error logging files which are being kept in when the application is installed or simply initialised from the source code.
On the windows platform, to define the the database path I used the following command:
appDataPath = os.environ["APPDATA"] + "\\FOLDERNAME\\"
But on my mac, I am getting the following stack trace:
Traceback (most recent call last):
File "main.py", line 9, in <module>
from view import tick
File "/Users/myname/Desktop/APPNAME/view/tick.py", line 8, in <module>
from controller import get
File "/Users/myname/Desktop/APPNAME/controller/get.py", line 4, in <module>
appDataPath = os.environ["/Users/myname/Desktop"] + "\\FOLDERNAME\\"
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
raise KeyError(key)
KeyError: '/Users/myname/Desktop'
What can I do to make this work? I tried changing this to my desktop location but still no joy. Where does a mac store the applications data? On Windows as you may very well know, is in the Roaming directory.
Thanks in advance
os.environ is a dictionary containing environment variables. There is no variable called /Users/myname/Desktop. That's why you're getting a KeyError. But there is a variable called HOME.
>>> os.environ['HOME']
'/Users/NAME/'
That should work equally on windows and OSX.
Now, if you want to concatenate paths, you should use os.path.join:
>>> os.path.join(os.environ['HOME'], 'MYPROJECT')
'/Users/NAME/MYPROJECT'
Related
I sporadically get this error using Python 3.10 in Windows. I've tried installing 3.7.8 to get around the error (based on prior posts) but it persists.
It can happen with any module, including os. For example, I can perform the command os.chdir() successfully but then when I try os.listdir() I get the error message.
This works:
file_loc = r"C:\Temp1\Registry\R2_XML_Files" # source directory for Registry
os.chdir(file_loc)
But then when I run
os.listdir(file_loc)
The error is
>>> os.listdir(file_loc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [WinError 87] The parameter is incorrect: 'C:\\Temp1\\Registry\\R2_XML_Files'
I've also tried using the pathlib module to troubleshoot Windows path issues with
file_loc = pathlib.PureWindowsPath ("C:/Temp1/Registry/R2_XML_Files")
but I still get the error
I've also tried not changing the directory but this code also fails:
file_loc = r"C:\Temp1\Registry\R2_XML_Files" # source directory for Registry
os.listdir(file_loc)
>>> os.listdir(file_loc)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [WinError 87] The parameter is incorrect: 'C:\\Temp1\\Registry\\R2_XML_Files'
>>>
On a related note, I've also noticed when using the following code, I get a similar error, but only every 4 days. It runs for 3 days, then throws the same OSError. I can prevent the error by preemptively re-creating (in Windows) the folder "c:/Temp1/Covid"
dir_name = r"c:/Temp1/Covid"
extension = ".zip"
os.chdir(dir_name)
for item in os.listdir(dir_name): # loop through items in dir
if item.endswith(extension): # check for ".zip" extension
file_name = os.path.abspath(item) # get full path of files
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(dir_name) # extract file to dir
zip_ref.close() # close file
os.remove(file_name)
Is there some Windows metadata that allows Windows to see and rename folders but does not allow Python to do so?
Have also run into the problem trying to pip install some modules.
I work for a hospital and have escalated to IT Security and no one seems to be able to figure out why this is occurring. The error also occurs for my colleagues and it is a huge hassle trying to keep Python stable. Starting over with a new install and new virtual environments sometimes works but sometimes doesn't.
Thanks very much.
from pyrrd.rrd import DataSource, RRA, RRD
filename = 'test.rrd'
dataSources = []
roundRobinArchives = []
dataSource = DataSource(dsName='speed', dsType='COUNTER', heartbeat=600)
dataSources.append(dataSource)
roundRobinArchives.append(RRA(cf='AVERAGE', xff=0.5, steps=1, rows=24))
roundRobinArchives.append(RRA(cf='AVERAGE', xff=0.5, steps=6, rows=10))
myRRD = RRD(filename, ds=dataSources, rra=roundRobinArchives, start=920804400)
myRRD.create()
Executing the above code, gives this error
rrdtool create test.rrd --start 920804400 --step 300 DS:speed:COUNTER:600:U:U RRA:AVERAGE:0.5:1:24 RRA:AVERAGE:0.5:6:10
Traceback (most recent call last):
File "test.py", line 10, in <module>
myRRD.create()
File "build/bdist.linux-x86_64/egg/pyrrd/rrd.py", line 175, in create
File "build/bdist.linux-x86_64/egg/pyrrd/backend/external.py", line 49, in create
File "build/bdist.linux-x86_64/egg/pyrrd/backend/external.py", line 21, in _cmd
pyrrd.exceptions.ExternalCommandError: /bin/sh: 1: rrdtool: not found
I am new to rrd tool. Kindly Help me to fix this. Thanks in advance.
You've described two different ways to create rrd file: one through the python package pyrrd, and one using the command-line tool, named rrdtool.
You don't need to use both - if you want to create the file using the python script, just run it like a regular script.
When you download pyrrd with apt-get (on ubuntu), it downloads also the cmd tool, while if you download it using pip, it downloads only the python package.
Since using your code on my environment work (no special PYTHONPATH configuration), I assume you have some installation problem, try re-installing, or installing the cmd tool separately.
I'm running Python (x,y) 2.7 on windows 7 32 bit and R version 3.1.0. I've been trying to install Rpy2 and have been getting many errors. I finally found this site which has pre-compiled python modules for windows http://www.lfd.uci.edu/~gohlke/pythonlibs/, so I downloaded rpy2‑2.4.2.win32‑py2.7.exe. When I did this and tried
import rpy2.robjects as robjects
I had an error saying it could not find R_HOME, so I updated my path variables. This was fixed, but then I got an error saying it could not find R_USER. Once again, I updated my PYTHONPATH variables based on SO responses. This didn't work, and so I'm stuck. I've updated my PYTHONPATH both inside Spyder and also in my system variables, but still no luck. Does anyone know what could be going on? This is the error I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\rpy2\robjects\__init__.py", line 18, in <module>
from rpy2.robjects.robject import RObjectMixin, RObject
File "C:\Python27\lib\site-packages\rpy2\robjects\robject.py", line 5, in <module>
rpy2.rinterface.initr()
RuntimeError: R_USER not defined.
This is what my PYTHONPATH includes:
C:\Python27\Lib\site-packages\rpy2;C:\Program Files\R\R-3.1.0\bin\i386;C:\Python27\Lib\site-packages\rpy2\robjects
This is what my PATH includes:
C:\Python27\Lib\site-packages\PyQt4;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;c:\Program Files\Intel\DMIX;C:\Program Files\Intel\Services\IPT\;C:\Python27;C:\Python27\DLLs;C:\Python27\Scripts;C:\Python27\Lib\site-packages\vtk;C:\Python27\gnuplot\binary;C:\Program Files\pythonxy\SciTE-3.1.0;C:\Program Files\pythonxy\console;C:\MinGW32-xy\bin;C:\Program Files\R\R-3.1.0\bin;C:\MinGW32-xy\mingw32\bin;C:\MinGW32-xy\bin
Thanks for any help you can provide!
You need to set the R_USER environment variable, e.g. to the username of the Windows account you use. See also this quote from this link:
1) Add the path to R.dll to my PATH variable (I went to the 32-bit directory) 2) Add an environment variable R_HOME (C:\Program Files\R\R-2.12.1 for me) 3) Add an environment variable R_USER (simply my username in Windows).
So I am having trouble compiling a very simple python script using JPype.
My code goes like:
from jpype import *
startJVM(getDefaultJVMPath(), "-ea")
java.lang.System.out.println("hello world")
shutdownJVM()
and when I run it I receive an error saying:
Traceback (most recent call last): File "test.py", line 2, in
<module>
startJVM(getDefaultJVMPath(), "-ea") File "/usr/lib/pymodules/python2.7/jpype/_core.py", line 44, in startJVM
_jpype.startup(jvm, tuple(args), True) RuntimeError: Unable to load DLL [/usr/java/jre1.5.0_05/lib/i386/client/libjvm.so], error =
/usr/java/jre1.5.0_05/lib/i386/client/libjvm.so: cannot open shared
object file: No such file or directory at
src/native/common/include/jp_platform_linux.h:45
I'm stuck and I really need help. Thanks!
I had the same problem
RuntimeError: Unable to load DLL [/usr/java/jre1.5.0_05/lib/i386/client/libjvm.so], error = /usr/java/jre1.5.0_05/lib/i386/client/libjvm.so: cannot open shared object file: No such file or directory at src/native/common/include/jp_platform_linux.h:45
In my case wrong JAVA_HOME path was set
/profile/etc
export JAVA_HOME
JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64
PATH="$JAVA_HOME/bin:$PATH"
export PATH
The work around is to define the full path directly in the call to the JVM:
from jpype import *
startJVM('/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/MacOS/libjli.dylib', "-ea", "-Djava.class.path=/tmp/Jpype/sample")
java.lang.System.out.println("Hello World!!")
shutdownJVM()
Original text:
Similar issues when trying to run JPype on MacOS El Capitan. I could
not figure out how to coax the _darwin.py code finding the correct JVM
location, despite the JAVA_HOME system variable being set properly.
Caveat cursor, trying to run the above code in the Spyder IPython console did not produce any output, but the normal Console would.
This Drives me nuts...
I use the latest EPD Distribution on the latest Mac and I want to study the book "Python for software design". Therein, Chap. 4, is one should load to play around with.
But to do that I seemingly need to set a PATH variable to the relevant directory. As I am not very familiar I looked around and finally added this to my .bash-profile:
"PYTHONPATH="$HOME/Dropbox/Programming/swampy-2.0"
The check with the Terminal:
Andreass-Mac-mini:~ ak$ $PYTHONPATH
-bash: /Users/ak/Dropbox/Programming/swampy-2.0: is a directory
Up to this point I would say it should work. And then comes this:
>>> from TurtleWorld import *
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from TurtleWorld import *
ImportError: No module named TurtleWorld
It is the right file name, the correct directory and it seems to me that the path is not right?
So what do I have to do/change that it works on my Mac?
Edit: and by the way - (on a different) I also created a directory .MacOSX and put the file environment.plist therein with the same path variable. Nothing changed :-(
Thanks
Andreas
Use the export command in your .bash-profile to make environment variables appear in other contexts:
export PYTHONPATH=$HOME/Dropbox/Programming/swampy-2.0
Did you name your script Turtleworld.py? If so, that is the issue. The module can't be imported if your script name is the same as the module name. Hope this helps! :D