PYTHONSTARTUP definition

On page 8 of the Python pocket reference, there are several “environment variables”. I don’t find any of them in my env var listing. I am most interested in PYTHONSTARTUP at this time. I moved all my Python files to a new location and everything seems ok. But when I open the Python shell, I get the message, "Could not open PYTHONSTARTUP file not found error [Errno 2] No such file or directory, ‘c:\Users\nkoss\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\pythonrc.py’ and yet, that file is indeed in that location. Anybody know what is going on here?

Welcome to the forum. A lot more information is needed in order to understand the situation properly.

I assume you mean, that some book that you are using to learn about Python, tells you about these environment variables.

Yes; they normally aren’t set. There are many possible environment variables that Python can use, as documented. If they aren’t provided, Python just ignores it or assumes a default. That’s how environment variables normally work for programs.

Exactly what does this mean? Which “files”? The code that you wrote? The Python installation itself? A virtual environment? Something else? Exactly where did you move them, and how did you do this?

Also, what problem did you hope to solve by moving the files? What is the underlying motivation for all of this?

Exactly what does this mean? Did you use a CMD window and type python and hit enter? Did you choose to run IDLE from the Start menu and try to use a “Python shell” window inside that program? Are you using some other IDE (such as Visual Studio Code, PyCharm, Spyder etc.)? Something else?

Please copy and paste, exactly and completely the text that you are getting - including any command that you type to start Python, and the prompt for that command (or else explain how you are getting there). Read the pinned thread first, and format the text like multi-line code. This will make it possible for us to actually understand what is happening. So far, it looks like you are using Visual Studio Code, but I would prefer not to have to guess this. It also isn’t clear what steps you’re taking within the program - are you trying to use a REPL? Trying to run some source code file (which one?)? Something else?

Thank you for your response. But I think you could be a little kinder to someone trying to learn how to program in Python. I will try to answer each of your queries.

  1. I had thought that the Python Pocket Reference was a book in common use. I guess I was wrong. In any case, there are a bunch of env variables all beginning with PYTHON, but none appear in my user or system list of env variables.
  2. I moved the files to a new location on my HD just because I didn’t like the way my system was set up. I did not get this message before.
  3. I guess the term “Python shell” is the wrong term. Given the Windows prompt, I entered python in the terminal of VS Code and received the message, “Could not open PYTHONSTARTUP file not found error [Errno 2] No such file or directory, ‘c:\Users\nkoss.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\pythonrc.py’” That is exactly the message I received. The term REPL confuses me. When I enter python at Windows prompt, is that an entry to REPL?
  4. I would just like to know where the message is coming from and how to get rid of it.

Thank you

It’s not possible to properly explain “where the message is coming from and how to get rid of it” without understanding the answers to the other questions. This is not something that I am arbitrarily asking in order to make your life more difficult. It is something that I need to know (and expect that anyone else would need to know) in order to be able to understand and solve the problem. Without these things, it is only possible to guess.

A REPL is what you get when you run Python without arguments (like in VS Code’s terminal).

I can’t help understand why “moving the files to a new location” caused a problem, without understanding what files were moved. But you get an error message like this from Python (whether it’s run from VS Code or not) because the PYTHONSTARTUP environment variable gives a path to a file that doesn’t exist. If you think there should be a file in the location c:\Users\nkoss.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\pythonrc.py, but your computer disagrees, I’m not sure what help you’re hoping to get from others.

From looking at that path, I can make a wild guess: the user account on your computer is actually named nkoss, not nkoss.vscode. Visual Studio Code wants the files to be in a .vscode folder that’s inside your user directory. In that case there’s a simple typo there (missing a \ to separate nkoss from .vscode).

If you don’t want to use any startup file, then you shouldn’t set the environment variable. Most likely, Visual Studio Code creates a file for you and puts some things there that it wants to happen when Python starts up. To fix this you’ll need to work through Visual Studio Code’s settings.

As for the Python Pocket Reference, I had not heard of it before. In general you should not assume that any book is “in common use” when it comes to programming - things are usually online now. I can’t find any evidence of any more recent edition of the Python Pocket Reference than the 5th edition which is now 10 years old or so. (I do recognize the name of the author as someone who thinks the language has been going downhill since about that time.)

I’m afraid I can’t give you a satisfactory explanation why I moved my workspace. As far as that apparent missing slash in the file location, I can’t explain why it appears that way, but it is correct in the system and the file is located there. You are so right about the pocket ref. Mine is the 5th edition…ancient history!! I have been searching the web to see if I could find what that file is that says it is missing when it isn’t. So far no luck. Thanks again

I forgot to ask. What book(s) would you suggest?

Ah, that results from the forum’s formatting, then. This would be an example of why I wanted you to read the pinned thread, copy and paste information and properly format it.

I guess that doesn’t matter now. What matters is: exactly what do you mean by “my workspace”? That is: which files were moved? What command or operation did you use to move them?

Assuming that the files that were moved include the PYTHONSTARTUP file, Visual Studio Code will need to know where it was moved to, in order to start using it again.

I do not suggest using any books. I suggest following the tutorial provided by the official Python online documentation and otherwise looking up any other information you need either directly in that documentation or by using a search engine. Or, of course, continuing to ask questions here :slight_smile:

The reason you can’t find anything on the Internet about that file (I assume you’re trying to search for it based on its name) is that there isn’t really anything special about it in itself. It’s just some Python source code which you could look at yourself (assuming you can find it… ?). The interesting thing is the fact that PYTHONSTARTUP is used to tell Python to run that code before doing other things.

The computer is telling you very clearly that you do not actually have a file in the location described in that error message. If you think you do, then we need to see what your evidence is in order to proceed.

I’ve been doing some reading and I found that PYTHONSTARTUP is a virtual env variable and its contents (a Python program) is run before anything else. I found the function to get and set the variable and further found that its contents are wrong. So I ran the setter to set the correct value, checked it and all looked correct. However, I then closed by doing ctrl-Z and return, so I was back at the Windows prompt. I then entered Python and the original error returned. Clearly, something else happened to reset PYTHONSTARTUP back to its incorrect value. I’m still puzzled.

Normally, we set environment variables by using the operating system itself:

It shouldn’t be necessary to use a “function” (I assume you mean, in your own Python code) for this.

I can’t understand what you mean by “closed” (closed what?) or by “entered Python” (make sure you understand that the Python program itself is separate from Visual Studio Code.

But I guess that Visual Studio Code is setting the value for you. You can set a current value for any environment variable in the current environment - but every terminal window (including the one that Visual Studio Code provides) has its own environment with its own environment variables. If you use Windows to set some environment variables, those are only defaults that get applied to each new terminal window; but after that anything can happen.

Learning Python, 5 ED, by Mark Lutz

This book is VERY thorough and comprehensive. There is a lot of reading but it will give you a richer understanding of the Python language (IMHO). The only critique that I have is that it is a bit dated (2013 - so it still makes a lot of references to Python v2.X). You can use this book as your ‘curriculum’ (guide) so to speak and every now and then take a detour if you need a 2nd opinion (i.e., explanation from another source(s) if you wish or need/like to do so).

This book teaches all of the fundamentals of the Python language. Note that since it is a bit dated as I stated, it doesn’t cover match-case statements (came online in v3.10). But overall, it is a very good book. All of the topics are explained fairly well.

The author also has a follow up to this book called Programming Python. It is primarily intended for the application of Python. The books should be read in this order.

Just my two cents.

Good luck on your Python journey. :foot::foot:

I did try using the Windows ‘set’ statement, but got the same result. The env variable seems to be set correctly, but when I enter ‘python’ (I guess that enters the REPL), the error returns. Something else is happening before the file in PYTHONSTARTUP runs since that is the only way the value could change after I had set it correctly.

What is the output of the following command? From a Windows Terminal:

DIR "c:\Users\nkoss\.vscode\extensions\ms-python.python-2024.2.1\pythonFiles\"

That command led nowhere since it had errors. But, be that as it may, a new day has brought a new success. Today, everything works fine, so I am not going to rock the boat. Thanks to all for offering suggestions