examine the variables when running python script in python environment? - python

I run my python script after launching Python environment in bash. Then I want to examine a variable defined in my script. But I cannot. I wonder how I can run the script in Python while still being able to examine the variables defined in the script after finish running? Note that I don't want to write the values of the variables to a file or stdout. Thanks!
>>> import myscript
>>> myvar
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'myvar' is not defined

You need to say that the variable is part of your import:
import myscript
myscript.myvar
or
from myscript import *
myvar

Related

Getting conda to apply to python script called by other python script

I have a script, A, that is importing another python script, B from the directory 'scripts'.
Both have the line :
from pymol import cmd, stored
And the conda environment I use to call script A has pymol activated.
However, pymol is failing to load within B, despite loading fine in A:
Traceback (most recent call last):
File "A", line 7, in <module>
import B
File "scripts/B", line 6, in <module>
from pymol import cmd,
stored ModuleNotFoundError: No module named 'pymol'
I've tried adding pymol to the path in B with
sys.path.append(os.path.abspath("/path/to/mambaforge/envs/pymol/bin"))
but am still getting the same error.
My question is should I be expecting this to work? Or is there something I need to do that I don't realise in order to get a conda environment to apply to a python script called by another script within the desired environment?

Running python file within a shell script : module not found error

I'm beginner to both bash and python.
I'm working in Ubuntu env.
to be short, I've created a shell script 'format_data.sh' that runs python file 'proc_ko.py' within it.
#!/bin/bash
...
python path/to/python/file/proc_ko.py
...
And the python file 'proc_ko.py' imports a module called khaiii
from khaiii import KhaiiiApi
api = KhaiiiApi()
...
But when I try to execute 'format_data.sh', I get this import error from python file.
Traceback (most recent call last):
File "media/sf_projet/pe/pe/PROGRAMME/SCRIPTS/proc_ko.py", line 5, in
from khaiii import KhaiiiApi
ImportError: No module named khaiii
which doesn't occur when I execute python file independently.
Python file 'proc_ko.py' itself doesn't have any error and 'khaiii' is well installed.
so I don't understand why import error occurs only through the shell script.
If u need more details to figure out, I'll be happy to provide. Thanks in advance for help.

No Module Found python (run outside IDE)

I'm trying to run a script outside pycharm and this is what happens
my command prompt when i try to run the main.py
C:\Users\Beatriz\PycharmProjects\Tese\Main> python main.py
Traceback (most recent call last):
File "main.py", line 1, in <module>
from View.view import *
ModuleNotFoundError: No module named 'View'
C:\Users\Beatriz\PycharmProjects\Tese\Main>
I put each file in a package like this:
files_packaging
Still it seems not recognize the module "View"
my main code:
from View.view import *
if __name__ == "__main__":
View()
The best way is to move the contents of the Main folder outside of it, directly inside the Test folder where all the other folders are. That way you can directly invoke from View.view import *.
To run the script:
cd PycharmProjects/Tese
python main.py

Run a Python script from Python prompt such that variables are loaded into the interactive environment

Say I have a (somewhat pointless) Python script
#!/usr/bin/python
a = 5
Is there a way to run this script from the interactive prompt such that after running if I type a I get
>>> a
5
and not
>>> a
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined
More generally, if my script calculates something through a series of steps, is there a way to access that something after the script has finished (in the same kind of manner).
Import it:
from yourscriptname import a
Each and every .py file in python is a module, and you can simply import it. If the file is called foo.py, import foo.

Unable to debug interactively with Python 2.7

I had asked a question previously on relative paths in python in SO question: How can I access relative paths in Python 2.7 when imported by different modules
The provided answer worked great in all of my scripts and functions. However, when trying to debug the files in IDLE (Python 2.7) it generates run time errors.
Can anyone point me to documentation on using the __file__ notation? Also I would like to understand why IDLE generates errors while running the sample code but running the same file from the command line or double clicking it (for the windows users) does not.
Any help would be greatly appreciated!
Note that I am running Python 2.7 on Windows XP with virtualenv (unactivated during these tests).
Sample Code
import os
import sys
curdir = os.path.dirname(__file__)
sys.path.append(curdir + '/..')
Error
Traceback (most recent call last):
File "C:\MyFile.py", line 3, in `<module>`
curdir = os.path.dirname(`__file__`)
NameError: name '`__file__`' is not defined
__file__ won't be set if you're writing this in the interpretor.
So:
>>> import os, sys
>>> curdir = os.path.dirname(__file__)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined
Is expected.
__file__ is the name of the file that was called by the python interpretor - so if you ran this from a script it would work.
$ python curdir.py
$
(The script is exactly the same as what I put into the interpretor, hence no error or output)
From what I've observed using IDLE before, it acts as an interpretor - so it'll run the file in question. However, it wasn't started with that file, so the __file__ is never set.

Categories