prickly-pythons / prickly-pythons

Our main code repository
11 stars 5 forks source link

Importing packages #15

Open kpolsen opened 9 years ago

kpolsen commented 9 years ago

Hey!

This issue is about importing packages to python and probably also a bit about preferred ways of running python.

So, I like to use vim in the terminal. With vim I edit my python script (python.py) which I then import in another tab/window to python. However, modules imported in the script are not carried over to the command line, which is kind of annoying. The screenshot gives an example:

screen shot 2015-10-01 at 5 15 22 pm

Any tips on how to start up python with some modules already imported? Or maybe YOUR favorite way of running python scripts?

johndwest commented 9 years ago

I write and run Python scripts in Geany. It's a text editor that acts like a lightweight IDE; write your code and then just click the run button, whereupon it opens a terminal window and runs the code. It works on Linux, Mac, & Windows, and is free. No problems with module importing. It color codes, auto-indents, and has rudimentary auto-complete capability. www.geany.org

screenshot

kpolsen commented 9 years ago

Thanks @johndwest! I will definitely try that out. Have you (or anyone else) tried out the Sublime text editor by any chance? http://www.sublimetext.com/ I haven't, but it also looks useful.

abhijithrajan commented 9 years ago

@kpolsen both @spacegal-spiff and I use sublimetext. It's really good and you can get really fancy with it.

johndwest commented 9 years ago

Interesting! I've never tried it. First impression is that multi-line editing could be a dangerous thing in my hands -- I could mess up a lot of code very quickly!

godber commented 9 years ago

I use sublimetext, many DesertPy members use sublime text.

When I set up someone new, I setup sublime text 3, install the anaconda plugin (not to be confused with the commercial python distribution) so they have pep8 and syntax checking. If you're using git, then gitgutter is another great plugin.

On Thu, Oct 1, 2015 at 6:03 PM, Karen Pardos Olsen <notifications@github.com

wrote:

Thanks @johndwest https://github.com/johndwest! I will definitely try that out. Have you (or anyone else) tried out the Sublime text editor by any chance? http://www.sublimetext.com/ I haven't, but it also looks useful.

— Reply to this email directly or view it on GitHub https://github.com/sese-python-users/sese-python-users/issues/15#issuecomment-144891407 .

abhijithrajan commented 9 years ago

Oh wow Austin I think you should do a session on this. Setting up sublime text 3 including github. I've wanted to do this but have been lazy.

On Thu, Oct 1, 2015 at 10:22 PM, Austin Godber notifications@github.com wrote:

I use sublimetext, many DesertPy members use sublime text.

When I set up someone new, I setup sublime text 3, install the anaconda plugin (not to be confused with the commercial python distribution) so they have pep8 and syntax checking. If you're using git, then gitgutter is another great plugin.

  • Austin

On Thu, Oct 1, 2015 at 6:03 PM, Karen Pardos Olsen < notifications@github.com

wrote:

Thanks @johndwest https://github.com/johndwest! I will definitely try that out. Have you (or anyone else) tried out the Sublime text editor by any chance? http://www.sublimetext.com/ I haven't, but it also looks useful.

— Reply to this email directly or view it on GitHub < https://github.com/sese-python-users/sese-python-users/issues/15#issuecomment-144891407

.

— Reply to this email directly or view it on GitHub https://github.com/sese-python-users/sese-python-users/issues/15#issuecomment-144894272 .

piyanatk commented 9 years ago

Regarding your importing issue, it is because you have not import numpy. By importing test, you import test.np, not np.

I used mainly used Pycharm which is a full-fledge Python IDE with a very good error checking, debugging and remote deployment tools when I developed most of api codes. For scripting, I use sublime when I have direct access to the graphic, or nano or emacs when when I have to use command-line.

On Oct 1, 2015, at 6:29 PM, Abhijith Rajan notifications@github.com wrote:

Oh wow Austin I think you should do a session on this. Setting up sublime text 3 including github. I've wanted to do this but have been lazy.

On Thu, Oct 1, 2015 at 10:22 PM, Austin Godber notifications@github.com wrote:

I use sublimetext, many DesertPy members use sublime text.

When I set up someone new, I setup sublime text 3, install the anaconda plugin (not to be confused with the commercial python distribution) so they have pep8 and syntax checking. If you're using git, then gitgutter is another great plugin.

  • Austin

On Thu, Oct 1, 2015 at 6:03 PM, Karen Pardos Olsen < notifications@github.com

wrote:

Thanks @johndwest https://github.com/johndwest! I will definitely try that out. Have you (or anyone else) tried out the Sublime text editor by any chance? http://www.sublimetext.com/ I haven't, but it also looks useful.

— Reply to this email directly or view it on GitHub < https://github.com/sese-python-users/sese-python-users/issues/15#issuecomment-144891407

.

— Reply to this email directly or view it on GitHub https://github.com/sese-python-users/sese-python-users/issues/15#issuecomment-144894272 .

— Reply to this email directly or view it on GitHub https://github.com/sese-python-users/sese-python-users/issues/15#issuecomment-144895343.

kpolsen commented 9 years ago

Ok, now I am really curious about Sublime. Oh thanks @piyanatk , I realise that I'm not importing numpy on the command line - I was doing that intentionally because I didn't want to type it every time. So I was asking for an easy way to carry over the imports made in the script to the command line... but maybe Sublime will solve that issue for me.

bajoshi commented 9 years ago

@kpolsen a different text editor may or may not solve your problem.

With the line "import test" all the modules in test are referenced to test. So if you want to use numpy you will need to type test.np But if you don't wan't to do that every time to use numpy (or any other module) then you can type "from test import *" then you can use numpy by just typing np

kpolsen commented 9 years ago

Thanks @bajoshi ! That helps a lot - so in stead of typing "import test" I will use "from test import *" and not have to type "test" nor import packages on the command line. Great

piyanatk commented 9 years ago

NO. NEVER EVER USE from module import *. You will be polluting your name space with variables. The suggestion that Bhavin and I made is a proper way. The best way to do it though is to use __init__.py file.

I normally just re-import numpy. It is better to be explicit.

Also, you should probably use iPython.

On Oct 2, 2015, at 1:22 PM, Karen Pardos Olsen notifications@github.com wrote:

Thanks @bajoshi https://github.com/bajoshi ! That helps a lot - so in stead of typing "import test" I will use "from test import *" and not have to type "test" nor import packages on the command line. Great

— Reply to this email directly or view it on GitHub https://github.com/sese-python-users/sese-python-users/issues/15#issuecomment-145145067.

bajoshi commented 9 years ago

In this case where you're only importing a module just to save yourself from having to write out all your import lines every time, I think it is okay if you use "from ImportFile import ". The ImportFile has a namespace that you're familiar with and it won't be polluting your current namespace with names that you don't recognize. This is what Boom is cautioning against; if you were to use "from numpy import " for example then that would introduce names from numpy into your namespace that you have no idea about.

Take a look here https://docs.python.org/2/tutorial/modules.html The tutorial is quite thorough in explaining how to use modules and packages.

godber commented 9 years ago

I am in agreement with Boom on this one. No using from foo import *. It's a bad habit that's easy to avoid and can really cause problems.

For instance, from numpy import * will override the python built in function sum() with numpy.sum() ... surprise!!!

>>> sum([1, 2, 3], 10)
16
>>> from numpy import *
>>> sum([1, 2, 3], 10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 1709, in sum
    out=out, keepdims=keepdims)
  File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", line 25, in _sum
    out=out, keepdims=keepdims)
ValueError: 'axis' entry is out of bounds
godber commented 9 years ago

@kpolsen, also as @piyanatk suggested, if you're looking to avoid importing things into interactive python sessions, use ipython ... and make a configuration for you default profile:

https://ipython.org/ipython-doc/dev/config/intro.html

the c.InteractiveShellApp.exec_lines is what you want to set. IPython is configured by "profiles", on my linux machine the path is:

~/.ipython/profile_default/ipython_config.py

you probably want something like this:

c = get_config()
c.InteractiveShellApp.exec_lines = [
    'import numpy',
    'import scipy'
]
kpolsen commented 9 years ago

Could one of you guys upload an example of init.py? Simply writing:

import numpy as np import pandas as pd

doesn't seem to work for me... am I missing something?

godber commented 9 years ago

If your goal is to always have nump as np and pandas as pd available in an interactive python session then use ipython, generate the configuration file with ipython profile create then

c = get_config()

...

c.InteractiveShellApp.exec_lines = [
    'import numpy as np',
    'import pandas as pd',
]

I think you need ipython rather than python.

piyanatk commented 9 years ago

And if your goal is to be able to write python scripts without having to write the numpy and pandas import lines, no, there is no good work-around.

If you omit the import lines but has your iPython configured like Austin said, and you execute your script in the iPython session (via execfile()), it will run. But if someone does not have their iPython configured, or if they execute it with the normal python shell, your script will fail.

Bottom line: be explicit and write the proper import lines.

__init__.py is for managing import in python packages. It can can be used to tell python which functions, classes, variables from which modules should be imported into another module in the same package, or when you import the package into your script or into a python session. It cannot be use to manage import for a single script (unless you make your script part of a package).

On Oct 5, 2015, at 2:57 PM, Austin Godber notifications@github.com wrote:

If your goal is to always have nump as np and pandas as pd available in an interactive python session then use ipython, generate the configuration file with ipython profile create then

c = get_config()

...

c.InteractiveShellApp.exec_lines = [ 'import numpy as np', 'import pandas as pd', ] I think you need ipython rather than python.

— Reply to this email directly or view it on GitHub https://github.com/sese-python-users/sese-python-users/issues/15#issuecomment-145679676.