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.
Related
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
I have previously only written python code using IDLE but since I am starting to do some more "heavier" programming I figured I should start using Visual Studio Code. I am however having issues doing things that I would like to do while coding to check that my functions are working as intended. The major thing I want to be able to do is if I have saved
def summa(x, y)
return x+y
in a file sum.py, then I would want to test run summa(3, 4) in the terminal.
In IDLE I am used to just running the file containing the function and then use it but I cannot figure out how to do that in Visual Studio Code. However, I realize that it is possible to import the file into a REPL terminal but I would hope that there is some easier way of doing it.
See the answers to this question :
execute python script with function from command line, Linux
You could also use the main function :
if __name__ == '__main__':
summa(sys.argv)
So you just launch the script and it run the function
You can start Python interactively and import your file as a module. Then your function will be available, so you can call it the way you want:
What I did was:
Open new terminal
Type: $ python to start an interactive session
Import module import test or the function from the module: import summa from test
Now I can call summa() and play with it in this manual manner
After installing matplotlib. I am getting this error while using this command :
$ import matplotlib
import-im6.q16: attempt to perform an operation not allowed by the security policy `PS` # error/constitute.c/IsCoderAuthorized/408.
Can anyone help me solve this?
This happens to me if I run import from the shell. Demonstration:
$ import
import-im6.q16: missing an image filename `import' # error/import.c/ImportImageCommand/1289.
This askubuntu question tells me that import is a built-in ImageMagick command.
Solution
You first have to start a Python interactive prompt:
python
>>> import matplotlib
Depends on how you do import matplotlib
The problem can be solved with just one of the following solutions.
If you're using the Python interpreter, the one from the command line, then this shouldn't happen because you are simply importing the module.
If you wrote the line of code in a Python file, suppose we call the file myfile.py, and you're trying to execute it on the command line like ./myfile.py then it's wrong because you have to write on the command line python myfile.py or python3 myfile.py, if you're using Python3
You can simply specify the python interpreter on your file by adding at the first line of the file #!/bin/python or #!/usr/bin/python and then on the command line you can simply run your file like a bash file ./myfile.py
You are calling import matplotlib not from the python shell, but from the system shell. The error you are seeing is from the import command, which according to man import saves a portion of a screen to an image. This also happens if you execute a script by calling it without putting a shabang on the first line (like #!/usr/bin/env python3)
See the other, older answers. They are very good.
Another possible reason: It might be a cut-and-paste-from-clipboard error.
You may have blanks before your #!/usr/bin/env python3 line, and at the beginning of all following lines. Thus your script is still a valid python-script but the shell cannot interpret the #!/usr/bin/env python3 line. It thus reads your script as a bash script.
It might help to remove the whitespace; or call the script with
python3 myscript.py
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()