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('.')
Related
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 12 months ago.
i am wondering if it is possible to open an app with python. I did some googling but i did not find anything that works. I tried:
import os
os.system("Settings")
and
import subprocess
subprocess.Popen("C:\Program Files (x86)\Steam\steam.exe")
but these do not work and i cant find anything else
Thanks in advance!
Your code didn't work because the format of the path is wrong
import subprocess
subprocess.Popen("C:\\Program Files (x86)\\Steam\\steam.exe")
or
import subprocess
subprocess.Popen(r"C:\Program Files (x86)\Steam\steam.exe")
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.
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:
python refresh/reload
(7 answers)
How do I unload (reload) a Python module?
(22 answers)
Closed 8 years ago.
I ran my script from Python environment launched in bash:
>>> import myscript
I then modified my script a little and save it. Then run again
>>> import myscript
But it doesn't run the updated script.
How can I tell Python to run the updated one? Thanks!
Simply reload it like this
reload(myscript)
Quoting from the docs,
Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter.
reload builtin is what you actually need:
https://docs.python.org/2/library/functions.html#reload
This question already has answers here:
Auto-load a module on python startup
(5 answers)
Closed 9 years ago.
I'm using python on Windows. And I'm trying to find a way to import some default module when starting python. It means, when starting python, some modules should be already imported, just like builtins. Is there any way?
Thanks.
If you mean while going into the interactive mode, just make a small startup script. Instead of just launching python with:
python
try instead:
python -i startupImports.py
startupImports.py:
import time
from collections import defaultdict
from customclass import myclass
#...etc
Define a PYTHONSTARTUP environment variable and point it to a python file and it will be loaded when you start python interactively without a command line script.
I have my PYTHONSTARTUP point to a .pythonrc where I try to import a __boot__.py (bootstrap) in the current directory for any interactive python session.
try:
from __boot__ import *
except Exception:
pass
There's a bunch of other things in my pythonrc like __future__ imports, readline setup, etc.
One option is adding your import to sitecustomize.py. This file is automatically imported at startup. This will be loaded whether Python is started in interactive mode or not. To quote the docs:
[In site.py] an attempt is made to import a module named sitecustomize, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in the site-packages directory. If this import fails with an ImportError exception, it is silently ignored.
Another option is to set the PYTHONSTARTUP environment variable to a startup script with your import. This will only be executed if Python is started in interactive mode, though.