Appearance of extraneous characters in ipython - ipython

What is with the extraneous characters showing up before I am even finished typing, i.e., “ ….: “?
In [21]: with open('os_walk_align.txt', 'w') as f:
In [21]: with open('os_walk_align.txt', 'w') as f:
....: for root, dir, ....: for ....: ....: ....: ....:
....: for root, dir, file in os.walk(spath):
....: pri ....: print(roo ....: print(root, file=f)
....:
python 3.4.3
visual studio code 1.6.1
ipython 3.0.0
ps - I looked at http://ipython.org/ipython-doc/rel-0.9.1/html/interactive/reference.html but I typed this in by hand, I did not paste. I also realized Ipython is now up to 5 for 2.7 and 6 for <3.3, so I will update that.

Related

how to debug CUDA,TensorRT in vscode

Environments: ubuntu20.04 + vscode(cmake) + CUDA + TensorRT
There are 2 ways to specify paths of include files and libs of CUDA+TensorRT:
1, in CMakeLists.txt
2, in vscode's c_cpp_properties.json
my questions are:
1, if I want to debug the program in vscode, both 1 and 2 are need to specify header files and lib files of CUDA and TensorRT, or just choose 1 or 2 is ok
2, I want vscode to recognize the system paths of CUDA I have written in
~/.bashrc, /usr/local/cuda-11.3/targets/x86_64-linux/include
should I write in c_cpp_properties.json manually?

can not import correctly package when have the same package-name in vscode

In my workspace, there are several directores(projects). All the directories have the same stucture, like :
project1:
docs/
src/
__init__.py
code1.py
test/
projects2:
docs/
src/
__init__.py
code2.py
test/
projects3:
docs/
src/
__init__.py
code3.py
...
# .env file in workspace
# PYTHONPATH=project1:project2:project3
When i want import package from code2, it would fail, such as code3.py
# code3.py
# from src import code2
I know in pycharm , it is easy to do with this situation by just marketing the directories as source root directory.
How can i do with it?
VS Code version: Code 1.43.2 (0ba0ca5, 2020-03-24T07:34:57.037Z)
OS version: Darwin x64 18.0.0
I have solved it by install extensions: Python/Python extension Pack/magic python
Mainly the Python Extension Pack.
When this extension enabled, others enabled, also, when it is disabled, others disabled also.

Eclipse + PyDev says json.load() and json.dump() do not exist

The following lines in Eclipse + PyDev with Python 3.4 work, but it is highlighted as Undefined variable from import: dump error:
import json
with open('C:\\test', 'w') as outfile:
json.dump([1, 2, 3], outfile)
Here is a snapshot of the Eclipse editor:
How do I get rid of that error?
EDIT
The output of:
import json
with open('C:\\test', 'w') as outfile:
json.dump([1, 2, 3], outfile)
print(json.__file__)
Is:
C:\Python34\lib\json\__init__.py
Here is the list of attributes that Eclipse shows:
problem in line 313 in file ../lib/json/__init__.py
...
if s.startswith(u'\ufeff'):
...
change this line to
if s.startswith('\ufeff'):
or wait for the new version
I was not able to get Eclipse to work, but I was able to get rid of the error (and of similar error/warnings): select the highlighted word, press ctrl+1 and select the line containing #UndefinedVariable to add a comment at the end of the row that will tell Eclipse to ignore that error on that line.
I use the same technique to ignore other errors with other modules, and it helps keeping the project clean.
See here for more details.
I had the same problem, but I had a file called json.py. Renaming my file to jsonTest.py from Eclipse left behind a json.pyc. Deleting that file did not help. Doing a Project->Clean did not help. Finally, I created a new workspace and things now work as expected. This was with Eclipse Kepler Release 2.
Adding json to the list of Forced Builtins of your Python Interpreter in PyDev Preferences also fix this issue.

How do I upgrade kernmagic to fix this ipython error?

I recently upgraded my Enthought Python distribution with
sudo easy_install -U ipython
However, when I start up ipython with the ipython command, I get an error:
David-Faux-MacBook-Air:core davidfaux$ ipython
Python 2.7.2 |EPD 7.2-2 (32-bit)| (default, Sep 7 2011, 09:16:50)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
[TerminalIPythonApp] Error in loading extension: kernmagic
Check your config files in /Users/davidfaux/.ipython/profile_default
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/ipython-0.13.1-py2.7.egg/IPython/core/extensions.pyc in load_extension(self, module_str)
90 __import__(module_str)
91 mod = sys.modules[module_str]
---> 92 return self._call_load_ipython_extension(mod)
93
94 def unload_extension(self, module_str):
/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/ipython-0.13.1-py2.7.egg/IPython/core/extensions.pyc in _call_load_ipython_extension(self, mod)
122 def _call_load_ipython_extension(self, mod):
123 if hasattr(mod, 'load_ipython_extension'):
--> 124 return mod.load_ipython_extension(self.shell)
125
126 def _call_unload_ipython_extension(self, mod):
/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/kernmagic/__init__.py in load_ipython_extension(ip)
28
29 def load_ipython_extension(ip):
---> 30 activate(ip)
31 activate_aliases(ip)
/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/kernmagic/__init__.py in activate(ip, *args)
14 continue
15 magic_name = name[len('magic_'):]
---> 16 ip.shell.define_magic(magic_name, getattr(mymagics, name))
17
18 def activate_aliases(ip, *args):
AttributeError: 'TerminalInteractiveShell' object has no attribute 'shell'
I googled around, and many posts I found said to comment out this line in an ipython configuration file.
c.TerminalIPythonApp.extensions = ['kernmagic']
I find it sketchy to comment out an ipython file though... later, I found that kernmagic fixed the issue with ipython.
How do I upgrade kernmagic in my ipython distribution so that this error goes away?
This worked for me:
hg clone https://bitbucket.org/robertkern/kernmagic
cd kernmagic
python setup.py build
python -c "import setuptools;execfile('setup.py')" bdist_egg
egginst dist/kernmagic-0.0.0-py2.7.egg
You can comment/remove the line in your configuration file, there is no issues with that if you do not use kernmagic. Actually the .ipython/profile_xxx folder are made for people to change things.
Otherwise if you really want to upgrade kern magic, I suggest you uninstall, remove the files and reinstall it from the current source.
Also IIRC the kern magic issue does not come with raw IPython only with EPD, so you shouldn't be afraid of removing it from config files and system.
For those who are not running EPD's distribution of python (or if Rich's solution didn't work):
First of all, I'm running Python 2.7.5 |Anaconda 1.6.0 (x86_64)|, on Mac OS X 10.8.4.
As Matt has stated, it is easiest to simply comment out the line that looks like this:
# A list of dotted module names of IPython extensions to load.
c.TerminalIPythonApp.extensions = ['kernmagic']
I found this in:
~/.ipython/profile_default/ipython_config.py
It should be located around 1/3 of the way down. It's worth pointing out that this is a temporary solution - I have no idea what kernmagic does or if it's an important ipython extension.
Enthought has updated kernmagic, so Enthought's enpkg fixed the same problem for me on EPD 7.3 and ipython 0.13.1:
sudo enpkg kernmagic
I've just created a quick-and-dirty port of kernmagic to python3. It may also work when this problem arises under python-2.x.
Here is a link to the code. All you need to do is drop the entire kernlab folder into your site-packages directory. It seems to be a pure python library, so it ought to work on any platform.
Good luck!
I also faced this problem, I'm on windows environment, I found that line in
C:\Users\Li\.ipython\profile_default\ipython_config.py
As stated, it is easiest to simply comment out the line that looks like this:
# A list of dotted module names of IPython extensions to load.
c.TerminalIPythonApp.extensions = ['kernmagic']

How to import python modules from parent and sibling packages

This (or similar) question has been asked many times before, but none of the solutions offered work in my case.
My project structure is like this :
| project_2
main.py
__init__.py
systems.py
| config
__init__.py
options.py
| database
__init__.py
database.py
entity.py
| tests
__init__.py
test_systems.py
test_options.py
test_database.py
test_entity.py
Obviously I need to import all the modules in the test modules under the tests package. I tried relative imports with the dot syntax:
from ..systems import System
from ..config import options
from ..database.entity import Entity
Returns a ValueError: Attempt relative import in non-package. I have tried that with a package structure where everything (including systems) is in its own package. It fails with the same message.
What really bothers me is that this is supposed to work: PEP 328, but it does not. I really want to avoid having to append the packages to $PYTHONPATH or to use some insane method such as loading the modules with imp from the file path.
I read that part of the problem might be that systems.py is in the main package, but that does not explain why the rest of the relative imports do not work either.
P.S. I actually recreated the example from PEP 328 just to test it and it does not work.
You get that when a python file does a relative import, but that file not loaded as a module via import in another module (but e.g. from the commandline). Given this structure:
.
├── main.py
└── test
├── __init__.py
├── a.py
└── b.py
main.py:
from test.a import A
print A
a.py:
from .b import B
A = B
if __name__ == '__main__':
print A
b.py:
B = 'b'
Now try:
python main.py
result is
b
and with
python test/a.py
you get:
Traceback (most recent call last):
File "test/a.py", line 1, in <module>
from .b import B
ValueError: Attempted relative import in non-package
What does work is:
python -m test.a
If you simply add . to your python path, if you run the script from the project_2 folder relative paths such as config.options will work. This requires an update to PYTHONPATH on every machine, unfortunately.
Tested on Python 2.7.14