Work with environment variables in Python [duplicate] - python

This question already has answers here:
How can I access environment variables in Python?
(16 answers)
Closed 2 years ago.
How can I set my DATABASE_URL as an environment variable in Python on Windows?

With the os module of the standard library;
>>> import os
>>> os.environ.get('NON_EXISTANT')
None
>>> os.environ.get('HOME')
/home/username
If you want to set such a variable;
>>> os.environ['YOUR_VARIABLE'] = 'your_value'
Note: print all available environment variables with os.environ.

Related

Find path of a package (e.g. pandas) in Python [duplicate]

This question already has answers here:
How to retrieve a module's path?
(23 answers)
Closed 2 years ago.
Is there a way to programmatically find the location of a python package once it is imported?
For example:
import os
import pandas
os.get_package_directory(panda)
>>> C:\Users\username\miniconda3\pkgs\pandas-1.0.5-py37h47e9c7a_0
It's been a while since I've done this, but try this:
import pandas
print(pandas.__file__)

ImportError: No module named site [PYTHON] -- Error message appeared after working with Python for a while [duplicate]

This question already has answers here:
"ImportError: No module named site" on Windows
(17 answers)
Closed 2 years ago.
Currently when I run a Python code (in Sublime Text 3) or try to run python in the cmd, I always get this:
C:\Users\Leopo\Flask App>python
ImportError: No module named site
In my user variables under the environment variables in PATH it is written: C:\Users\Leopo\AppData\Local\Programs\Python\Python38-32\Scripts\ and C:\Users\Leopo\AppData\Local\Programs\Python\Python38-32\
And in the system variables under PATH it is written:
%PYTHON_HOME%
with PYTHON_HOME : C:\Users\Leopo\App Data\Local\Programms\Python38
I already repaired and reinstalled Python 3.8.3.
Is there maybe a problem regarding the environment settings?
Thanks for the help!
This problem has occurred to almost everyone
In my case, my system needed a restart to fix the problem

Python: How to list imported modules [duplicate]

This question already has answers here:
How to list imported modules?
(10 answers)
Closed 3 years ago.
In Python calling the import function brings in a module to give a program access to its namespace of classes, functions and variables.
import os
import sys
print(os.name)
print(sys.implementation)
Is there a way to list which modules have already been imported in a program?
>>> list_imports()
sys
os
And if so, is there a way to figure out the version of the imported module?
sys.modules is the closest you'll get. Modules following spec will have a __version__ attribute.
Just use sys library
import sys
sys.modules.keys()

The dot in module import (python 3.x) [duplicate]

This question already has answers here:
What does a . in an import statement in Python mean?
(3 answers)
Closed 4 years ago.
For what i can use the "." in import module. I meet it in sklearn library. It looks like:
from .externals import six
The . is a shortcut that tells it search in current package before rest of the PYTHONPATH. So, if a same-named module Recipe exists somewhere else in your PYTHONPATH, it won't be loaded.

How to change C drive to F drive using python Script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to set the current working directory in Python?
How to change C:\> to F:\> using Python
I tried
import os
os.chdir("F:")
but got an error.
You need the extra backslash:
import os
os.chdir("F:\\")

Categories