Running script file from Python interpreter - python

In Python interpreter (Python 3.9.2, Win 10) I've already run
import numpy as np
In a plain text file "myscript.py" in the same (current working) directory is the single line
A = np.array((1,2,3,4))
Running at the interpreter
import myscript
gives the error message (in part)
NameError: name 'np' is not defined
I'm sure it's a namespace-ish thing; I'm a long-time R user just starting to explore Python. Just puzzled why np isn't defined, despite having imported that alias at the interpreter previously.
I'm looking for something equivalent to R's source() function whereby I can have the Python interpreter and the source code text file both open, make changes to the source code file, and rerun it in the interpreter with each such change. (I'm aware of using importlib.reload() for the subsequent re-runs, but the "name undefined" issue keeps me from getting out of the starting gate.)

If you import myscript in the interpreter, Python just executes that code (similar to using python3 myscript). If you haven't put import numpy as np at the top of your script, then np is not defined. Because it is not defined.
So to start off, you need to add that line to myscript.py:
import numpy as np
A = np.array((1,2,3,4))
Then you can run your script interactively in the interpreter with
>>> exec(open('myscript.py').read())
or, as you suggested, with
>>> import importlib
>>> import myscript
>>> importlib.reload(myscript)
The first two lines must be at the start of your interpreter session, and everytime you edit myscript.py you run importlib.reload(myscript).

Related

Import on shared object fails with ModuleNotFound only when script is executed from command line

ran into a weird problem where there is a shared-object import error only when I run the script from command line. It succeed if i run the script in the python console using exec(...)
I have a class that needs a shared object: foo.py:
import os
cur_dir = os.curdir()
os.chdir('/tmp/dir_with_shared_object/')
import shared_object_class
os.chdir(cur_dir)
class Useful:
... # use the shared object import
Then there is a script like this:
from foo import Useful
If I enter python console and run:
exec(open('script.py').read())
Everything works fine.
If I run this on command line:
python script.py
I will get
ModuleNotFoundError: No module named 'shared_object_class'
The python is the same. It is 3.7.3, GCC 7.3.0 Anaconda. Anyone knows what is causing this discrepancy in behavior for shared object import?
A standard way of importing from a custom directory would be to include it in the PYTHONPATH environmental variable, with export PYTHONPATH=/tmp/dir_with_shared_object/.
update
It could also be done dynamically with
import sys
p = '/tmp/dir_with_shared_object/'
sys.path.insert(0, p)
PS
I think I have an explanation for why OP's original code didn't work. According to this python reference page, the import system searches, inter alia, in "[t]he directory containing the input script (or the current directory when no file is specified)." So the behavior in the REPL loop is different from how it is when running a script. Apparently the current directory is evaluated each time an import statement is encountered, while the directory containing the input script doesn't change.

pandas.DataFrame.describe() gives no output in .py script

I am following Google's Machine Learning Crash Course, so that I can move on to TensorFlow. However, when I try to execute the code in First Steps With TensorFlow, I get no output from the following line:
california_housing_dataframe.describe()
Here is my full code in case it helps:
import math
from IPython import display
from matplotlib import cm
from matplotlib import gridspec
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
from sklearn import metrics
import tensorflow as tf
from tensorflow.python.data import Dataset
tf.logging.set_verbosity(tf.logging.ERROR)
pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.1f}'.format
california_housing_dataframe = pd.read_csv("https://storage.googleapis.com/mledu-datasets/california_housing_train.csv", sep=",")
california_housing_dataframe = california_housing_dataframe.reindex(np.random.permutation(california_housing_dataframe.index))
california_housing_dataframe["median_house_value"] /= 1000.0
california_housing_dataframe.describe()
So far, I have tried the following:
Execute the .py file with the python command.
Execute the .py file with the ipython command. I also tried to use arguments -i -c (i.e. ipython -i -c "%run filename.py").
Open ipython.exe and use the run command to execute my script.
Open ipython.exe and copy the code line by line.
Out of the above, only copying every line individually into IPython gave proper output. Is there a way to execute my script without having to copy every line in IPython?
Within a program only the function creating the summary in describe() is executed; in a console environment silently the result is automatically printed as well, as this is what you typically want to see there.
In your program you would have to call
print(california_housing_dataframe.describe())
explicitly.
The interactive python console, jupyter console etc are geared up to allow you to type a variable name, or it's attribute to display it. This is not the case when running a script non-interactively. Instead you must use the print() function.
print(varaible_name.attribute)

how to import python module inside IDE

I have this line of import in my python code:
from project.lib.framework.test_cases import TestCase
and it works fine when I run it from command line. However, if I try to run it from my IDE (Active state komodo), I get an error:
ImportError: No module named project.lib.framework.test_case.
Can anyone tell me why?
Because you changed the import statement?
In the first example, you are importing from project.lib.framework.test_cases, but in the second, you appear to be importing from project.lib.framework.test_case. Notice the missing s at the end.
Other then that, assuming you are using the same python binary, there should be no difference between an IDE and a command line for import statements.

How do I import a py module into the command line

I am using the Anaconda command prompt, with Python - in Windows 8.
I have a py file containing containing a number of "Helper Functions".
How do I import this file in to the command prompt I am using so future operation will recognize the function calls and execute them appropriately?
If by command prompt, you mean that the python interactive shell and you want to import the module every time you start the interactive shell, you can use the .pystartup file to import the relevant modules so that they are always available when you start the interpreter.
On Linux, you can edit the file /home/username/.pystartup to add:
import mod1
import mod2
Have an environment variable which points to the ~/.pystartup file
$ export PYTHONSTARTUP=~/.pystartup
And then use the python interactive shell use the modules loaded in ~/.pystartup
$ python
>> mod1.something
On Windows, you can do the same by adding the environment variable PYTHONSTARTUP with value some_windows_path (I keep mine in C:\python27\pystartup.py) via Computer->Advanced Settings->Environment variables.

Run python script from python using different python

I have a software that has python 2.5.5. I want to send a command that would start a script in python 2.7.5 and then proceed with the script.
I tried using
#!python2.7.5
and http://redsymbol.net/articles/env-and-python-scripts-version/
But I cant get it to work...
In my python 2.5.5 I can execute script as
execfile("c:/script/test.py")
The problem is that the 2.7.5 has a module comtypes + few other. I dont know how to install it for my 2.5.5 so I'm trying to start a separate script and run it under python27. Now another reason why I want it its because I want to take the load off program. I have 2 heavy tasks to perform. The second task is the one that need comptypes so sending it to external shell/app would do perfect trick. Is there a way to do it ?
I wish I could just type run("C:/Python27/python.exe % C:/script/test,py")
Thanks, bye.
Little update. I try to run
import os
os.system("\"C:\Python27\python.exe\" D:\test\runTest.py")
But I'm getting a quick pop up and close window saying that
Import Error : no module named site...
This works if I run from external shell but not from here :(
So I've tried another approach this time to add modules to python... in any case I run this :
import os
import sys
sys.path.append("C:/python27")
sys.path.append("C:/Python27/libs")
sys.path.append("C:/Python27/Lib")
sys.path.append("C:/Python27/Lib/logging")
sys.path.append("C:/Python27/Lib/site-packages")
sys.path.append("C:/Python27/Lib/ctypes")
sys.path.append("C:/Python27/DLLs")
import PyQt4
print PyQt4
import comtypes
import logging
but it crashes with C error...
Runtime Error :
Program: c:\Pr...
R6034
An application has made attempt to load the C runtime library incorectly.
blablabla....
How can I import it ? Maybe if I can import it I can run it directly from my app rather than starting separate python...
Traceback (most recent call last):
File "<string>", line 18, in <module>
File "C:\Python27\Lib\site-packages\comtypes\__init__.py", line 22, in <module>
from ctypes import *
File "C:\Python27\Lib\ctypes\__init__.py", line 10, in <module>
from _ctypes import Union, Structure, Array
ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.
Another update to isseu
so I run now
import os
os.system("start cmd {D:\test\runTest.py}")
now this works and he open CMD with c:\Python27 as directory but he dont run the file... any hitns how to fix it?
Use "raw" strings so that you don't need to escape as much; I think the backslashes are what was breaking your code since backslash is considered an escape character except in raw strings.
Also, use the subprocess module. It makes it easy to avoid manually making a safe command string (the module takes care of that for you). All you need to do is pass it a list of arguments.
Your code would then look something like this:
import subprocess
proc = subprocess.Popen([r"C:\Python27\python.exe", r"D:\test\runTest.py"])
# then either do this
proc.wait() # wait until the process finishes
# or this
while True:
# NOTE: do something else here
# poll the process until it is done
if proc.poll() is not None:
break # break out of loop
See subprocess docs for Python 2 here. Be sure to check if a feature was added after Python 2.5 (the 2.5 docs aren't available online anymore AFAIK).
UPDATE:
I just noticed that you tried to use the Python 2.7 libraries and modules in your 2.5 code. This probably won't work due to new features added after 2.5. But it got me thinking how you might be able to make 2.7 work.
It may be that your Python2.7 install can't find its libraries; this is probably why you get the error Import Error : no module named site. You can do something like the above and modify the PYTHONPATH environment variable before starting the subprocess, like this:
import os
import subprocess
paths = [r"C:\python27", r"C:\python27\libs", r"C:\python27\Lib\site-packages", r"C:\python27\DLLs"]
paths += os.environ.get('PYTHONPATH', '').split(os.pathsep)
env27 = dict(os.environ)
env27['PYTHONPATH'] = os.pathsep.join(paths)
proc = subprocess.Popen([r"C:\Python27\python.exe", r"D:\test\runTest.py"], env=env27)

Categories