Can I define all import in one file in python shell - python

I'm trying to import multiple imports in one module, and then import that module and have everything I need imported. Can I do that?
I'm trying it like this:
>>> os.system('cat python_imports.py') # just to show the contents of a file
import sys
0
>>> import python_imports # now import
>>> sys # now use the imported stuff
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
So what to do if this doesn't work?
If someone didn't understand the question: When working with python shell; I'm using some same modules everytime, which need to be imported manualy. I don't want to import the same modules every time I start Python shell, I want to import them faster.
So is there any way of doing this?
Thanks!

It looks like you just want a convenient way to import modules into a shell session without a lot of tedious typing.
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> open('python_imports.py').read()
'import sys\n\n'
>>> from python_imports import *
>>> sys
<module 'sys' (built-in)>
>>>

Related

Why does Python allow importing from a local module, but not importing the module itself? [duplicate]

I always thought, doing from x import y and then directly use y, or doing import x and later use x.y was only a matter of style and avoiding naming conflicts. But it seems like this is not always the case. Sometimes from ... import ... seems to be required:
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> PIL.__version__
'6.1.0'
>>> im = PIL.Image.open("test.png")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'PIL' has no attribute 'Image'
>>> from PIL import Image
>>> im = Image.open("test.png")
>>>
Am I doing something wrong here?
If not, can someone please explain me the mechanics behind this behavior? Thanks!
For submodules, you have to explicitly import the submodule, whether or not you use from. The non-from import should look like
import PIL.Image
Otherwise, Python won't load the submodule, and the submodule will only be accessible if the package itself imports the submodule for you or if some previous code has explicitly imported the submodule.

Is "from ... import ..." sometimes required and plain "import ..." not always working? Why?

I always thought, doing from x import y and then directly use y, or doing import x and later use x.y was only a matter of style and avoiding naming conflicts. But it seems like this is not always the case. Sometimes from ... import ... seems to be required:
Python 3.7.5 (default, Nov 20 2019, 09:21:52)
[GCC 9.2.1 20191008] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> PIL.__version__
'6.1.0'
>>> im = PIL.Image.open("test.png")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'PIL' has no attribute 'Image'
>>> from PIL import Image
>>> im = Image.open("test.png")
>>>
Am I doing something wrong here?
If not, can someone please explain me the mechanics behind this behavior? Thanks!
For submodules, you have to explicitly import the submodule, whether or not you use from. The non-from import should look like
import PIL.Image
Otherwise, Python won't load the submodule, and the submodule will only be accessible if the package itself imports the submodule for you or if some previous code has explicitly imported the submodule.

How to import a package that use absolute import in python

I'm trying to import volatility3 into my python project/script, so that I don't have to use os.system since volatility3 is already made in python3.
I'm wondering how can I import all the functions/modules of said project ? The functions I'm interested in are located in volatility3/volatility/framework
I tried simply putting:
>>> import volatility3.volatility.framework
But I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/volatility3/volatility/framework/__init__.py", line 12, in <module>
from volatility.framework import constants, interfaces
ModuleNotFoundError: No module named 'volatility'
My guess is I have to modify sys.path or one of the path variables but this does not seem to work.
Thanks,
The best solution here would be to properly install volatility with pip3, from your already exiting repository folder:
$ pip3 install /home/volatility3
or directly from pipy (not tested):
$ pip3 install volatility3
then you should be able to import the from volatility package directly:
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import volatility.framework
>>> volatility.framework
<module 'volatility.framework' from '/home/bruno/.local/lib/python3.6/site-packages/volatility/framework/__init__.py'>
>>>

psutil can not be found within a flask project

I have setup a fairly simple flask project. Within this flask project I want to use psutil but its not being found with the import statement.
> $ ./satelite.py
Traceback (most recent call last):
File "./satelite.py", line 2, in <module>
from app import app
File "/home/neil/monitor/satelite/app/__init__.py", line 4, in <module>
from app import views
File "/home/neil/monitor/satelite/app/views.py", line 6, in <module>
import psutil
ImportError: No module named psutil
However when I use python cli it is.
> $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import psutil
>>> print psutil.cpu_percent()
15.3
The same can be said for just a straight forward python script being executed separately.
Standard python modules(subprocess, os, etc) are loading fine. I have tried to focus my google-fu on this but I am getting nowhere so would massively appreciate if anyone can point me in the right direction.
Cheers
Does your first line in satelite.py refers to the same python binary as which python in your terminal ?
(This refer to the #! line)
Maybe you are using python3 in your satelite.py file.

ImportError in IDLE

I consider myself an intermediate user in python, and this one is a new one. Testing code in IDLE (Python 3.2.3) on Linux. Here is the entire script:
Python 3.2.3 (default, Apr 10 2013, 05:29:11)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
==== No Subprocess ====
>>> from os import listdir, getcwd, chdir
>>> chdir('Documents/matrix')
>>> getcwd()
'/home/bradfordgp/Documents/matrix'
>>> listdir('.')
['__init__.py', 'vec.zip', 'hw1.pdf', 'politics_lab.pdf', 'submit_hw1.py', 'submit_politics_lab.py', 'test_vec.py', 'Week1', 'Week0', 'python_lab.py~', 'Week2', 'vec.pdf', '__pycache__', 'hw1.zip', 'politics_lab.zip', 'voting_record_dump109.txt', 'my_stories.txt~', 'hw1.py', 'politics_lab.py', 'submit_vec.py', 'vec.py']
>>> from vec import Vec
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
from vec import Vec
ImportError: No module named vec
>>>
I've navigated to the correct directory, and I'm importing from the same directory, and vec.py exists. Why is in not locating a file in the local directory? Running this script from a terminal window command line works correctly.
Suggestions?
use this..
import sys
sys.path.append("/home/bradfordgp/Documents/matrix")
import vec
import sys
sys.path.append("/home/bradfordgp/Documents/matrix")
import vec
Rather than change to the directory, just add the location to the places python will search on import
See more here
In interactive mode, import will try to import modules from the current directory after os.chdir. But in non-interactive mode, it will fail and still search from the previous directory. You can see more discussion from this issue. In non-interactive mode, you'd better do what others mention.

Categories

Resources