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.
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:
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:
Passing options to Python executable in non-interactive mode
(4 answers)
Closed 7 years ago.
I would like to execute a script work.py in Python, after executing some initialization script init.py.
If I were looking for an interactive session, executing python -i init.py or setting PYTHONSTARTUP=/path/to/init.py would do the trick, but I am looking to execute another script.
Since this is a generic case which occurs often (init.py sets environment and so is the same all of the time), I would highly prefer not referencing init.py from work.py. How could this be done? Would anything change if I needed this from a script instead of from the prompt?
Thank you very much.
More generally than in the accepted answer of C0deH4cker, this is discussed in the Python manual in Section 2.2.5 - Cusomization Modules. The basic idea is, to get the location of the special start-up script, one needs to execute the following Python code, e.g. from the interactive session of the interpreter:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'
The output should be exactly such a location, in the file sitecustomize.py.
Python has a special script that is run on startup. On my platform it is located at /usr/lib/python2.5/site-packages/sitecustomize.py IIRC. So, you could either put init.py in that directory alongside a sitecustomize.py script that imports it, or just paste the content of init.py in the sitecustomize.py.
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