0

Most of my work with python uses almost the same modules as well as default settings so I want to do this once for all. This is what I did: 1. put export PYTHONSTARTUP=$HOME/.pythonrc into ~/.bashrc file 2. put the following code into the /.pythonrc file

    from pylab import *
    import os
    rc('font', **{'family': 'serif', 'serif': ['Computer Modern']})
    rc('text', usetex=True)

However, when I run the following code python3 test.py with the test.py containing the simple code like this:

    os.chdir('/Users/username/Documents/python/test/')
    datadax=loadtxt('py_dax.txt')
    datashaxn=loadtxt('py_shaxn.txt')
    datashaxw=loadtxt('py_shaxw.txt')

It gives me the error: NameError: name 'os' is not defined

I am not sure which step I did wrong.

yangyang
  • 125
  • 1
  • 6

1 Answers1

0

It seems what you want is only possible in interactive mode!

From the Python ยป Documentation: PYTHONSTARTUP

If this is the name of a readable file,
the Python commands in that file are executed before the first prompt is displayed in interactive mode.

Relevant: SO pythonrc in interactive code

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • Thanks. Indeed you are right. However, I also tired the interactive way. I typed python(3) in the terminal and I tried the command 'os.chdir('/Users/username/Documents/python/test/')'. The same error appears. Also I would like to know if there is a way to achieve this when I execute the whole *.py profile. โ€“ yangyang Jul 10 '17 at 21:06