add a permanent directory to PYTHONPATH bug - python

I have tried to add a permanent directory from here but i didn't understand how to do it.
The answer states that:
You need to add your new directory to the environment variable PYTHONPATH, separated by a colon from previous contents thereof.
However I pressed the Windows_Start Button and the Pause/Break Button and in Advanced System Settings I went to Environment Variables
AND HERE'S THE PROBLEM
There is no Variable named PYTHONPATH to set-up a value there!!! It must be a bug!
These are the values that exist in my System variables
ComSpec
FP_NO_HOST_CHECK
NUMBER_OF_PROCESSORS
OS
Path
PATHEXT
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVER
PROCESSOR_REVISION
PSModulePath
TEMP
TMP
USERNAME
windir
windows_tracing_flags
windows_tracing_logfile
See my problem is that i use sys.path.append() to add the directory but once i restart the GUI i must re-input the command.
The solution aforementioned and linked states that i must find the PYTHONPATH and add the directory there but i have no Environment Variable named PYTHONPATH
How can i run around this problem?
EDIT
Create a file start-my-app.cmd using a text editor (Notepad is good, Word/Wordpad is bad). Copy the code above and replace the parts between ... with what you need. Double click start-my-app.cmd or type start-my-app.cmd in a command prompt (you need to be in the same folder as start-my-app.cmd for this to work). – Aaron Digulla
-->Like this
THIS IS HOW I DID IT. IS IT CORRECT? PLZ TELL ME
Go to Environment Variables
Click the New Button in System Variables
Fill in the variable details
It looks suspiciously easy to me in contrast to the answers i have received but it works and every time i open the GUI i dont have to append the path. If there is anything wrong with this solution plz tell me.
Thank you

You can create a new one; the comment above only means: "If there already is one, don't just overwrote the current value".
That said, I don't like changing global environment variables much. First of all, you must not forget to restart all command prompts because existing ones don't get new variables.
My preferred solution is to create a .cmd/.bat file which contains:
set PYTHONPATH=...whatever your code needs...
python ...and start your Python code here...
That way the variable will be there when you expect it, no matter when and from where you start the script and it will not interfere with other stuff that you might also have.

Related

What is the use of os.getenv("HOME") in QFileDialog creation?

Like I said in the title I don't get what os.getenv("HOME") does in this code. I am following a course on an online site and the tutor was coding an interface with PyQt5 similar to notepad. I searched for an answer but they are a bit too advanced I guess. Also I have no idea what an environment variable is. By the way this is my first question on stack so excuse me for any possible mistakes and insufficient information.
def open_file(self):
file_name=QFileDialog.getOpenFileName(self,"Open File",os.getenv("HOME"))
with open(file_name[0],"r") as file:
self.writing_ar.setText(file.read())
The function above is connected to a button self.open such as self.open.clicked.connect(self.open_file)
And self.writing_ar is a QTextEdit object
In the case of os.getenv('HOME'), it's a UNIX-centric way to get the current user's home directory, which is stored as an environment variable per POSIX specification. A typical home directory location is /Users/yourname on MacOS, or /home/yourname on Linux, or c:\Users\Your Name on Windows -- so that's what this code is trying to look up.
The set of environment variables is effectively a key/value store, mapping strings to other strings, that is copied from any program to other processes it starts; they're thus a way to share configuration and other information between programs (though it only shares information down the tree, propagated only on process creation; changes made by a child process are not seen by its parent; and changes to a parent's environment after a child is started are not seen by the child).
If you want something that works reliably even on Windows, consider os.path.expanduser("~") instead. Thus, your code might become:
file_name = QFileDialog.getOpenFileName(self,
"Open File",
os.path.expanduser("~"))
See also What is the correct cross-platform way to get the home directory in Python?
It basically gets an environment variable for you and cast that onto a python variable.
From the code you shared, there should be a variable defined at the operating system level named HOME.
In Linux, that can be done with
export HOME="something_here"
You can check that this variable has actually been defined by typing
echo "$HOME"
in the terminal.
You can think of the os.getenv() method like it "echoes" the value of that argument onto some variable.

Cannot create urls.yaml in order to use urlwatch

I have installed urlwatch and I am trying to do the configuration. I am very, very new to python and the command line. I am having issues with urlwatch --edit which returns
Parsing failed:
[WinError 2] El sistema no puede encontrar el archivo especificado
The file C:\Users\HP\AppData\Local\urlwatch\urlwatch\urls.edit.yaml was NOT updated.
Checking the specific path, I cannot find the file urls.edit.yaml. In its place is urlwatch.edit.yaml and in the same folder, urlwatch.yaml. Both seem to be identical.
Running C:\Users\HP\AppData\Local\urlwatch\urlwatch>urlwatch --list, returns
You need to create C:\Users\HP\AppData\Local\urlwatch\urlwatch\urls.yaml in order to use urlwatch.
Use "urlwatch --edit" to open the file with your editor.
So I am moving in circles and not quite understanding what's going on. Any help is appreciated.
To clear any confusion regarding the urlwatch yaml files: There are two main configuration files used by urlwatch. General (non-job-specific) and reporter settings are established within urlwatch.yaml [1]. Jobs themselves are defined within urls.yaml [2].
As far as editing goes: urlwatch --edit will attempt to create/modify the urls.yaml file using the editor specified by the $VISUAL or $EDITOR environment variables [3]. If neither of these are set, that may be part of your problem, and you may want to try setting one of these. For example, if you would like to edit your configurations using Notepad on Windows, you might set the EDITOR environment variable to notepad before running the urlwatch --edit command, after which a new Notepad instance should pop up for you to enter your job configuration(s) into.
If you're having issues using --edit, or finding it cumbersome to mess with environment variables, it may be easier to manually create and populate the file yourself. In your case, it seems your urlwatch configuration should be located within C:\Users\HP\AppData\Local\urlwatch\urlwatch\. You should be able to create a new file called urls.yaml within this folder and edit it yourself using your preferred text editor.

Determine how environment variable was set

In Python I can access an environment variable as:
os.environ['FOO']
I would like to know if the variable was set previously via export or if it was set only for the current python script like so:
FOO=BAR python some-script.py
Basically I want to only use FOO if it was set like in the line above and not permanently defined per export.
Arguments to the python script itself unfortunately are no option here. This is a plugin and the parent application does not allow passing custom arguments it does not understand itself.
I was hoping I somehow could access the exact and full command (FOO=BAR python some-script.py) that started python but it appears like there is nothing like that. I guess if there was a feature like this it would be somewhere in the os or sys packages.
The environment is simply an array of C strings, there is no metainformation there which helps you find out whether or not the invoking shell had the variable marked for export or not.
On Linux, you could examine /proc/(pid)/environ of the parent PID (if you have suitable permissions) to see what's in the parent's permanent environment, but this is decidedly nonportable and brittle.
Spending time on this seems misdirected anyway; let the user pass the environment variable in any way they see fit.

Im in the beginning of the Flask tutorial for python, and I dont understand this paragraph

Usually, it is a good idea to load a configuration from a configurable file. This is what from_envvar() can do, replacing the from_object() line above:
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
That way someone can set an environment variable called FLASKR_SETTINGS to specify a config file to be loaded which will then override the default values. The silent switch just tells Flask to not complain if no such environment key is set.
I am not too familiar with environment variables. I would like an explanation of the above paragraph in simple terms. My best guess is that when the program reads FLASKR_SETTING does that mean that on my own computer I have set up a mapping to this file with that name with something called an environment variable? Ive messed with my environment path before and to be honest I still don't understand it, so I came here looking for a clear answer
Environment variables are a name,value pair that are defined for a particular process running on a computer (windows or UNIX/LINUX etc.). They are not files. You can create your own environment variables and give it any name/value. For example, FLASKR_SETTING is the name of the environment variable who value could be set to a config file. On a UNIX terminal for example, you can do:
export FLASKR_SETTING = /somepath/config.txt
By doing the above, you have just created an environment variable named FLASKR_SETTING whose value is set to /somepath/config.txt. The reason you use environment variables is because you can tie them to a certain process and use on demand when your process starts. You don't have to worry about saving them in a file. In fact, you can create a launch script for your process/application that can set a variety of environment variables before you starting using the application.
In case of flask, app.config.from_envvar('FLASKR_SETTINGS', silent=True) sets the value of FLASKR_SETTINGS to the value from the env. variable. So it basically translates to:
- Find the config file (/somepath/config.txt etc.)
- lets say the contents of config file is:
SECRET_KEY="whatever"
DEBUG = True
- Then using the 2 above, it will be translated to:
app.config['SECRET_KEY'] = "whatever"
app.config['DEBUG'] = True
So this way, you can just update the config file as needed and you will not need to change your code.
Environment variables are a simple, ad-hoc way of passing information to programs. On unixy machines, from a command shell, it's as simple as
export FLASKR_SETTINGS=/path/to/settings.conf
/path/to/program
This is especially useful when installing programs to start up at reboot; the configuration can be easily included in the same setup script that launches the system program.

How to point textmate to the same version of python I get at the command line

My Textmate uses python 2.6.1 and my system uses 2.6.5. This is not a big deal for the difference between them in terms of functionality, but all the packages that I installed with easy_install are not available to the Textmate version (2.6.1), though they are available to 2.6.5.
After researching this question I typed which python at the command line and learned that the desired python is /Library/Frameworks/Python.framework/Versions/2.6/bin/python.
I tried alternately adding a TM_PYTHON to the shell variables or a #! pointing to the path above. The result of each is the error msg IndexError: list index out of range, passed by module Q = sys.argv[1].
I cleared these out and tried creating a /usr/.profile with the line
alias python='/Library/Frameworks/Python.framework/Versions/2.6/bin/python'
with no effect.
Can anyone tell me what I am doing wrong?
Thanks in advance.
You are nearly there.
In the menubar, click TextMate, then select Preferences from the drop-down menu, and then click Advanced from among the options at the top of the pop-up window. Next, click Shell Variables from among the three items near the top of the Advanced pane.
menubar > TextMate > Preferences > Advanced > Shell Variables
Here, you want to add two variable-value pairs, one for PATH and one for PYTHONPATH, so at the bottom left, click the "+" to add the first of these Variable-Value pair. You can edit either Variable or Value field by clicking in the text area.
The Value you want for PATH is the:
/Library/Frameworks/Python.framework/Versions/2.6/bin
make sure this is the first directory that appears in the Value field for the PATH variable. If you want to add other directory paths to that Value field, separate it from the one you just added using a colon ":"
Next, create the variable PYTHONPATH and for the value, specify the directory that holds your site packages. It is probably this:
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages
but definitely verify this.
When you have added these two variable, make sure that you check the boxes for each in the far left column.
(You might need to re-start TextMate.)
Finally, make sure your hash bang line at the top of your python script template is consistent with what you just did--i.e., the directory set for the value in PATH should simlink to the path specified in your pound bang line. The path specified in your pound hash bang should be a symlink to the python executable.
You might also want to add the line below at the beginning of your script file:
#!/opt/local/bin/python
The lion above is the result of:
which python

Categories

Resources