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.
Related
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.
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.
I'm using the app QPython, and while it's easy to run scripts from a file, I'm struggling to see how to load a script into the Console so that I can use it there (e.g. to use functions defined in a script).
I'm not very familiar with Python, so I don't know whether I'm having difficulty with Python or with the app. As far as I know in ordinary Python, the command "import script" will import all of the code in the file script.py, which has to be contained in the directory you loaded Python from (this is already concerning as I can't change the directory in QPython).
For the record, the equivalent command in Haskell (which I am familiar with) would be :l script.hs
To import some functions :
from script import functiona, functionb()
To import all functions from a script use :
from script import *
You could just do :
import script
But then you'll have to call your functions like that :
script.myfunction()
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.
Here is the problem...
I'm writing very small plugin for Blender,
I have 10 python scripts, they parsing different file formats by using command-line, and I have a Main Python script to run all other scripts with proper commands...
for example, "Main.py" include:
txt2cfg.py -inFile -outFile...
ma2lxo.py -inFile -outFile...
Blender already include Python, so I can run "Main.py" from Blender, But I need it to work with both PC and MAC, and also doesn't require Python installation, so I can't use:
execfile(' txt2cfg.py -inFile -outFile ')
os.system(' ma2lxo.py -inFile -outFile ')
or even import subprocess
because they required Python installation in order to run *.py files.
Sorry for language
Thanks
If you really need to execute a python script in a new process and you don't know where the interpreter you want is located then use the sys module to help out.
import sys
import subprocess
subprocess.Popen((sys.executable, "script.py"))
Though importing the module (dynamically if need be) and then running its main method in another script is probably a better idea.
for example, "Main.py" include:
txt2cfg.py -inFile -outFile...
ma2lxo.py -inFile -outFile...
Two things.
Each other script needs a main() function and a "main-import switch". See http://docs.python.org/tutorial/modules.html#executing-modules-as-scripts for hints on how this must look.
Import and execute the other scripts.
import txt2cfg
import ma2lxo
txt2cfg.main( inFile, outFile )
ma2lxo.main( inFile, outFile )
This is the simplest way to do things.
Two options:
Use py2exe to bundle the interpreter with the scripts.
Import the modules and call the functions automatically.