Python: Threading script interfering with separate script - python

I have a script called startup_launching.py, which does something like this:
import os
# launch chrome
os.startfile(r'C:\Program Files (x86)\google\chrome\application\chrome.exe')
To run this from the (windows) command line, I enter:
python "FILEPATH\startup_launching.py"
That works fine.
However, I have a separate script called threading.py, which does this:
import time, threading
def foo():
print(time.ctime())
threading.Timer(10, foo).start()
foo()
(which I found on stackoverflow).
When threading.py is saved in the same folder as startup_launching.py, it seems to interfere with startup_launching.py when I run it from the command line (e.g. one of the error messages is: module 'threading' has no attribute 'Timer').
When I move threading.py to another folder, startup_launching.py works fine again.
Can someone explain what's going on here? I assumed that entering:
python "FILEPATH\startup_launching.py"
in the command line would only look in startup_launching.py
Thanks!

you should rename your file so that it is not named threading.py, since it will be in the import path and will mask the actual built-in threading module, which the other script relies upon.

Name your module something other than threading.py because there is a built-in module named threading.py.

Don't call it threading.py. Also, check your python version, if it correspond to the tutorial that you were reading.

Related

os.system() keeps giving me import errors

I am using os.system("python game2.py") to run different parts of my code.
Every time I try this it gives me an import error for example "no module named pygame" even though when I game2 itself, it works fine.
What can I do?
Like said in other answers there is no need to run os.system() if you want to import the code. You can instead use the import function in python.
However I think the reason for the error with the command may be to do with what happens to the directories you are in when you run that command.
When you run the command the directory you are in is the same directory as the code is being run from. Then when you run the game2.py code and it tries to import pygame the reason it can't find it is because it is only looking the cwd (current working directory) as it normally would however the cwd could be different in the os.system() command.
Well, first of all, you shouldn't even do that for code in different files, you can just do import game2 and then run the functions inside of it. As an example, game2.RunGame(), If you have a function called that, of course this is just an example

Importing a python module after execute a command on python

I work on python and I want to import a module just after execute a command with python in the same script. I need to execute the command before importing the module.
When I start to execute my command on a console, then I can open python and import MyModule but if I pass my command by python then import MyModule report in console :
ImportError: No module named MyModule
I already try this two methods :
subprocess.call()
os.system()
Any Idea ?
What I usually do and what I usually see other programmers do is that they import their libraries first, before they even put any line of code. This is also helpful to see what libraries you're working with since the beginning of the code.
import subprocess
import Mymodule
script = "command"
subprocess.call = (script, shell = True)
Please let me know if this fix the issue, here's also a cool article that might help you.

Importing from python interactive mode

I ran a script using "python -i" from the command line. The script ran as I expected and I end up in interactive mode, as expected.
Now, however, I want to use a command from the scipy.signal package, so I type:
>>> from scipy import signal
For some reason, this triggers the interpreter to run the whole script again from the start.
Why does this happen? And how should I avoid it?
When you import a file the whole file is read in and executed. This is the same whether you use from file import function or just import file.
You should place any code you don't want to run when it is imported in a block like this:
if __name__ = '__main__':
your code here
Your function definitions that you wish to import should be outside this block, as they need to be loaded and executed to be imported and available for use.
See this duplicate question which explains this in further detail.

importing the wx module in python

When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.

reload (update) a module file in the interpreter

Let's say I have this python script script.py and I load it in the interpreter by typing
import script
and then I execute my function by typing:
script.testFunction(testArgument)
OK so far so good, but when I change script.py, if I try to import again the script doesn't update. I have to exit from the interpreter, restart the interpreter, and then import the new version of the script for it to work.
What should I do instead?
You can issue a reload script, but that will not update your existing objects and will not go deep inside other modules.
Fortunately this is solved by IPython - a better python shell which supports auto-reloading.
To use autoreloading in IPython, you'll have to type import ipy_autoreload first, or put it permanently in your ~/.ipython/ipy_user_conf.py.
Then run:
%autoreload 1
%aimport script
%autoreload 1 means that every module loaded with %aimport will be reloaded before executing code from the prompt. This will not update any existing objects, however.
See http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html for more fun things you can do.
http://docs.python.org/library/functions.html#reload
reload(module)
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. The return value is the module object (the
same as the module argument).
An alternative solution that has helped me greatly is to maintain a copy of sys.modules keys and pop the new modules after the import to force re-imports of deep imports:
>>> oldmods = set(sys.modules.keys())
>>> import script
>>> # Do stuff
>>> for mod in set(sys.modules.keys()).difference(oldmods): sys.modules.pop(mod)
>>> import script

Categories

Resources