I have created the class in separate code in Python (Spyder). Then I import the class into main code using:
from othercode import classxy
Then the .pyc file is created in pychache' folder. However when I change something in the class, save it and import it once again in main code, then the .pyc file is not updated and main code is still working with old version of class. I have to delete the .pyc file for class and turn off and turn on the Spyder which is kinda dumb. Is there something I'm missing? When I run the main code in Anaconda prompt, everything is working as it should, just Spyder is behaving strange.
(Spyder developer here) To have your code updated after every change to it, you need to run these commands before running your code in our IPython consoles (but only once):
In [1]: %load_ext autoreload
In [2]: %autoreload 2
Related
Today I run my python(.py) file as usual,but the terminal shows a new problem which I cannot solve for a long time.
Explicitly,I used to click Run Python File,
but the terminal shows that like this:
I then tried another two ways to run my python,such as "run code"(code runner provides),debug(python plugin together with "run python file"),and it shows that:
.
It is obvious that except the "debug",other two both fail to run,showing "ModuleNot..."(while actually the module exists in the vir env).
All codes are easy:
print('Hello World')
import os
print(os.path.abspath(__file__))
import numpy as np
go to vscode Extensions market and search "python". right click on "Python"(published by microsoft) and click on "Install another version". select a version that previously worked for you - usually just the one back before the current one.
Faced with the same issue.Solved by adding the following line
python.terminal.activateEnvironment" : false
to the settings.json file of the visual code
In the new version of Python extension in VSCode you have to add the Path for the conda by yourself. Go to the Python extension settings and add the path for the conda.exe.
Screenshot of the needed setting
I felt so lost; what a strange error.
Yes, just install an earlier version of the vscode python extension.
I cannot understand how such a bug has been introduced into the extension.
This is somethings that has been bothering me since I started using PyCharm to program in Python. I have a two .py files, in the same directory, main.py and external.py. Inside main.py, I have import external at the top. PyCharm marks this as an error, but it runs fine both in the new zsh MacOS terminal and PyCharm itself, and I can use all the things declared in external.py as expected.
I've played around with it a bit, and (to my very limited knowledge) it seems that PyCharm detects imports like Python2. Thats a guess, though, as I am unfamiliar with that version.
Why does PyCharm do this, or am I the one to blame? If it's not my fault, how can I fix it?
My file structure is as follows:
Project-|
|-external.py
|-main.py
I want to use things from external.py in main.py, and I can, but PyCharm gives it a red underline.
From given description, it correctly imported your external module.
Did you create a folder inside your project folder?
When using subfolders for your main.py/external.py files, Pycharm might not by default correctly detect your import statement.
Pycharm should give you an error message for said import statement.
Maybe the error is not connected to the import statement but to your pycharm setup e.g. correctly setting up your python interpreter.
If you provide more information regarding your folder structure or the error message, that might help.
Please try to mark directory containing your python files as Sources Root, see https://www.jetbrains.com/help/pycharm/configuring-folders-within-a-content-root.html
I am a Matlab user and new to Python. I want to call the simplest python function from an external file. I edit the function, but my Canopy interpreter (IPython) does not recognize the new version and keeps calling the old file!
Say this function saved as mymodule.py:
def oper(x):
print(x)
The main file is:
del mymodule.pyc
import mymodule
import imp
imp.reload(mymodule)
oper(5)
Run this once. Change print(x) to print(x+1). It keeps executing print(x).
If I define the function oper(x) in the main file, or if I close Canopy and reopen it, or if I reset the Canopy kernel via GUI Run>Reset Kernel, it's all right (but I cannot program these).
I also tried reload(mymodule) and %reset, which do not solve the problem. Ideally, I want a Python command in the main file that would completely reset the workspace (kernels, namespctes etc). I am using Canopy 1.7.4.3348, which includes Python 2.7.11 and IPython 4.0.0.9.
You need to import like this and use like
from mymodule import oper
oper(5)
or
import mymodule
mymodule.oper(5)
Also, in the same directory of mymodule, create an empty __init__.py file there. That will let python know that it is a module.
http://pastebin.com/BJiXC022
At first my python is working just fine with tkinter. When I change the working directory, it somehow stops working then. It even manages to refer the tkinter.py file in that directory even when I never even typed the name of the file there. I just wanted to import tkinter. My tkinter.py file is also not working even though it is almost exactly the same as the first 10 lines. How do I fix this problem? I reinstalled os and python yesterday, I am running OS X 10.10.3 and the newest Python 3.4.3. Here's tkinter.py:
http://pastebin.com/VBHqFGLZ
You have a file named tkinter.py in /Users/nikolas/Documents/Python/tkinter.py. Changing to that directory and importing tkinter will import the local file, not the one from your Python installation. You see the error because your tkinter.py file does not provide Tk.
The solution is to rename your file to something other than tkinter.py.
Does anyone know if it is possible to load ipython preloaded with custom packages please?
I'm running Python 2.7 on Windows 8.
When I load a DOS prompt, I run ipython preloaded with pylab by typing
ipython --pylab
I've managed to create a shortcut to open a DOS prompt with this automatically fired, thus effectively creating a shortcut to iPython.
However, I'd like iPython to start preloaded with some of my custom packages. So I wonder if there is a way to start iPython and automatically execute the following lines, say:
import package1 as my_package
import package2 as my_second_package
I've had a look online and there's some information on "magic" commands and scripts in iPython which looks like it might help, although I wasn't sure how to use this.
All guidance welcomed.
Many thanks.
What you want is a startup script.
First run ipython locate profile to find the profile folder. Then find a startup folder in there. Create a .py file (any name) in the startup folder with the imports you want, and IPython will run that whenever you it starts.
If you have a look at the documentation, you will find out that IPython will run whatever is inside the file pointed by the PYTHONSTARTUP variable.
Create one, export the variable, and there you go.
References:
http://ipython.org/ipython-doc/stable/interactive/reference.html#ipython-as-your-default-python-environment
https://docs.python.org/2/using/cmdline.html#envvar-PYTHONSTARTUP