This question already has answers here:
What exactly is current working directory?
(5 answers)
Closed 6 months ago.
I have a python 3.6 script that I wrote in Atom editor on macOS. The script uses os.getcwd() routinely and has always worked fine. I restarted my computer last night, updated the Atom packages, and suddenly it broke. Using a print statement, os.getcwd() is now only returning "/".
Tried on multiple scripts, all in different directories.
import os
print(os.getcwd())
Expected: /Users/kpaddock/Desktop/Python/SCID-Report-DRAFT
Actual output: /
My mistake, I didn't look hard enough. Turns out os.getcwd() is not always accurate? Replaced with os.path.abspath(os.path.dirname(__file__)) and it works just fine.
Related
This question already has answers here:
Difference Between Python's IDLE and its command line
(4 answers)
Closed 1 year ago.
But they work just fine in IDLE. For example,
import time
time.asctime()
works fine in IDLE, but is not working in Pycharm.
I have tried changing the interpreter but still doesn't work. Other packages that I installed myself from cmd like numpy are working fine.
Any help will be appreciated
EDIT : When I add the package time by pressing + in the interpreter settings, it says
ERROR: Could not find a version that satisfies the requirement time
If you get no error message, and you have shown us your whole program then your problem is that you are expecting time.asctime() on its own to produce output the way it does in IDLE.
But to get the same effect in a program that PyCharm runs, you need something like print(time.asctime()).
To get PyCharm to do what IDLE does in this case, you need to ask PyCharm to open a Python console.
This question already has answers here:
Importing files in Python?
(6 answers)
Closed 7 years ago.
I am trying to import a file into Python shell. I have read multiple answers on this site to the same question asked by other users, but none of them have worked.
The command I type is:
import filename.py
I have also tried typing:
from filename import *
In IDLE I clicked File -> Path Browser
to make sure my file was saved in one of those folders.
Am I doing any steps wrong?
Look here: Importing files in Python?
Credit to Pradyun for his excellent answer:
If you are working in the same directory, that is, b.py is in the same folder as a.py, I am unable to reproduce this problem (and do not know why this problem occurs), but it would be helpful if you post what os.getcwd() returns for b.py.
If that's not the case, add this on top of b.py
import sys
sys.path.append('PATH TO a.py')
OR if they are in the same path,
import sys
sys.path.append(os.basename(sys.argv[0])) # It should be there anyway but still..
Please do further research before posting your answer.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Test if executable exists in Python?
Is there a python function to let me detect whether a program is installed in the computer. I have a program that runs an .exe, that part works on windows but to run it in linux you need wine so I need a way for the python function to detect wine.
You can use function os.get_exec_path() to get a list of directories which are set in PATH environment variable. If the executable you are looking for is not present in any of these directories it is correct to assume that the program is not installed.
The code snipped to determine whether there is Wine installed then would look like this:
import os
winePath = None
for directory in os.get_exec_path():
testWinePath = os.path.join(directory, "wine")
if os.path.exists(testWinePath) and os.access(testWinePath, os.R_OK | os.X_OK):
winePath = executablePath
break
If there is Wine installed then the path to its executable file (wine) will be in winePath variable; if not found winePath will be None.
The code is also checking whether the file has correct perrmisions for reading and executing it.
The os.get_exec_path() is available since Python 3.2. In older versions you can use os.environ["PATH"].split(":") instead.
This question already has answers here:
How to know/change current directory in Python shell?
(7 answers)
Closed 9 years ago.
Windows 7
As I'm learning Python (3.2) I prefer using IDLE than CMD.
In order to change the path where I can import scripts of my own, I use a little trick that I found in this site: I go FILE>OPEN>directory>myscript and then run it and from then on I'm on this directory.
Nevertheless, I wonder whether there is a simple command, like CD... to move to the correct directory, without using tricks.
Thank you,
http://www.skylit.com/python/Appendix-A.html
Check section A.2, just before section A.3:
>>> from os import chdir
>>> chdir("C:/myOtherWork")
And also to check the working directory:
>>> from os.path import abspath
>>> abspath('.')
If the current directory is not already in the path you need to add it:
>>> import sys
>>> sys.path.append('.')
This question already has answers here:
How do I get the path and name of the file that is currently executing?
(26 answers)
Closed 8 years ago.
Duplicate of: In Python, how do I get the path and name of the file that is currently executing?
I would like to find out the path to the currently executing script.
I have tried os.getcwd() but that only returns the directory I ran the script from not the actual directory the script is stored.
In Python, __file__ identifies the current Python file. Thus:
print "I'm inside Python file %s" % __file__
will print the current Python file. Note that this works in imported Python modules, as well as scripts.
How about using sys.path[0]
You can do something like
'print os.path.join(sys.path[0], sys.argv[0])'
https://docs.python.org/library/sys.html