How to add implicit imports to ipython? - python

I am using a the PYTHONSTARTUP=~/pythonstartup.py approach in order to make ipython the default python shell but I want to extend it do have some additional imports by default (like importing few common packages).
How can I do that?
#!/usr/bin/env python
import os
import sys
import re
import time
import platform
import IPython
os.environ['PYTHONSTARTUP'] = '' # Prevent running this again
IPython.start_ipython()
raise SystemExit

Problem solved by adding
IPython.start_ipython(user_ns=locals())

Related

How to make an import statement work on both python 2 and python 3

I'm doing a migration for a code from python 2 to python 3.
there is some code that I'm not migrating and is necessary to the code that i do migrating,
therefor I need that some of the import statements work on both versions but the package got its name changed for example:
import urlparse # Python2
import urllib.parse as urlparse # Python 3
how can i code on statement that will work on both versions.
keep in mind that this question is for the general case (the example above is only one of the problems created by the following migration)
For your imports, you can do the following:
import sys
if sys.version_info[0] < 3:
#import your py2 packages
else:
#import your py3 packages

Any way to include based on Python version

I need to detect Python version and execute code just for this version.
I want to use one file for each version of Python.
Use sys.version_info.
import sys
PY3 = sys.version_info[0] == 3
if PY3:
# execute code for python 3
import file_you_want_for_py3
else:
# execute code for python 2
import file_you_want_for_py2
If you're not sure which version of a module might be available, you can use a try/except:
try:
import new_shiny_module as module
except ImportError:
import old_rusty_module as module
the best and easiest way is to use tox like python library.

No module named Linq

I wrote a python script to use in C#, but when I run my project this error occurs: "No module named Linq". what should I do? I took the import part from another part of project from TFS and it works well there, so what is going on?
import System
import clr
import sys
clr.AddReference(''System.Core'')
from System import Array
from System import DateTime
from System.Linq import Enumerable
from System import Func
Check your single vs double quotes.
But try "ImportExtensions"
clr.AddReference("System.Core")
import System
clr.ImportExtensions(System.Linq)

Atom is rearranging my import lines on save

When working on a python script, Atom rearranges my lines by moving some import lines to the top
I only have the AutoPep8 plugin which I disabled and this still happens. I've also looked at the Whitespace plugin as well but it doesn't have the settings I'd expect that could cause this behavior:
Before save
#! /usr/bin/env python
"""
Random doc text
"""
import sys
import os
import math
package_path = os.path.dirname(os.path.dirname(__file__))
sys.path.append(package_path)
from parent import package
import scipy
On save
#! /usr/bin/env python
import scipy
from parent import package
"""
Random doc text
"""
import sys
import os
import math
package_path = os.path.dirname(os.path.dirname(__file__))
sys.path.append(package_path)
I can ctl-z to undo this. Is there a way to disable/fix this behavior?
Got the same problem, but restart of the atom after I disabled the autopep plugin worked for me. This looks like a bug in autopep8 plugin

Error with sys.path.append and module

I am a newbie with Python and I always run scripts with terminal. Now I would like to run and debug using PyCharm. I have this script:
# -*- coding: utf-8 -*-
matplotlib.use('Agg')
import sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import os
import math
import time
from time import sleep
import fpformat
sys.path.append("/Users/myname/OneDrive - UCL/my_folder/build")
from my_module_name import example
When I run the script with terminal everything works.
When I use PyCharm I have this:
/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 "/Users/myname/OneDrive - UCL/my_folder/Simulations/S16/03/04_Command_compacity/8.Compact_resistance.py"
Fatal Python error: PyThreadState_Get: no current thread
The problem is here:
sys.path.append("/Users/myname/OneDrive - UCL/my_folder/build")
from my_module_name import example
I have to use this module for my master thesis and if I remove these two lines I can run other scripts, but I can't run scripts that use this module.
Could you help me? Thank you!
I typed
which python
in terminal and I changed the directory in the Python Interpreter settings

Categories

Resources