Path to lib/pythonX.Y/no-global-site-packages.txt - python

What is the official way to get the path of lib/pythonX.Y/no-global-site-packages.txt?
I would like to guess and use sys.version in a way which is finally slightly different.

Looking at the source:
import site
site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))
no_global_file = os.path.join(site_mod_dir, 'no-global-site-packages.txt')

Related

Best way to handle data import paths independent of the PC I use

I am currently looking into working from different PCs on the same ownCloud data, doing file imports such as:
```data = pd.read_csv(r"C:\Users\User\ownCloud\Sample Information\folder1\folder2\data.csv")```
However, only "/Sample Information/folder1/folder2/data.csv" is independent of the PC that I use.
The Jupyter notebook would be somewhere like this: r"C:\Users\User\ownCloud\Simulations\folder1\folder2\data_model.ipynb"
I tried stuff like the following, but it wouldn't work:
```data = pd.read_csv(".../Sample Information/folder1/folder2/data.csv")```
Is there a concise way of doing these imports or do I need to use the os module and some combination of os.path.dirname( *... repeat for amount of subfolder until ownCloud is reached...* os.path.dirname(os.getcwd)) ?
Thanks for your help!
EDIT:
After some pointers I now use either of these solutions with v1 similar to this and v2 similar to this:
import os
ipynb_path = os.getcwd()
n_th_folder = 4
### Version 1
split = ipynb_path.split(os.sep)[:-n_th_folder]
ownCloud_path = (os.sep).join(split)
### Version 2
ownCloud_path = ipynb_path
for _ in range(n_th_folder):
ownCloud_path = os.path.dirname(ownCloud_path)
You could replace username in the path with its value by string interpolation.
import os
data = pd.read_csv(r"C:\Users\{}\ownCloud\Sample Information\folder1\folder2\data.csv".format(os.getlogin())

Connecting to a part of a website depending on input. Python

Im new to python and wondering if there is a way for it to open a webpage depending on whats been inputted. EG
Market=input("market")
ticker=input("Ticket")
would take you to this part of the website.
https://www.tradingview.com/symbols/'market'-'ticker'/technicals
Thanks
Looks like you were pretty much there, but it python you can use the + sign to concatenate strings and then cause it to open that link using webbrowser library
import webbrowser
market=input("market")
ticker=input("Ticket")
webbrowser.open('https://www.tradingview.com/symbols/'+market+'-'+ticker+'/technicals')
Its cleaner to use format string like this:
import webbrowser
market=input("market")
ticker=input("Ticket")
webbrowser.open(f'https://www.tradingview.com/symbols/{market}-{ticker}/technicals')

Python How can I get the version number from a .whl file

Because of some custom logging I would like to get the version number from a wheel file.
I could of course just parse the filename, but my guess is there would be a better way to do this.
You can also use pkginfo e.g.
from pkginfo import Wheel
w = Wheel(r'C:\path\to\package.whl')
print(w.version)
What I did for now is:
Get the package info and put it into a dict:
def get_package_info():
info_dict = {}
with open(os.path.join(glob.glob("./*.egg-info")[0], "PKG-INFO"), "r") as info:
for i in info:
i = i.split(":")
info_dict[i[0].strip()] = i[1].strip()
return info_dict
Now I can get the Verion from this dictionary.
If someone does have a better approach at this, please let me know.
A simple parsing of the wheel's filename should be enough:
>>> 'torch-1.8.1-cp39-cp39-manylinux1_x86_64.whl'.split('-')[1]
'1.8.1'
If you want to be more sophisticated, you can also use wheel-filename to do this parsing for you:
>>> from wheel_filename import parse_wheel_filename
>>> pwf = parse_wheel_filename('pip-18.0-py2.py3-none-any.whl')
>>> pwf.version
'18.0'
If you just need to compute it quickly for a single package, you can use this codepen page I created, to get version and more details from a wheel's filename: https://codepen.io/chaitan94/full/mdWEeBp

Error when implementing gensim.LdaMallet

I was following the instructions on this link ("http://radimrehurek.com/2014/03/tutorial-on-mallet-in-python/"), however I came across an error when I tried to train the model:
model = models.LdaMallet(mallet_path, corpus, num_topics =10, id2word = corpus.dictionary)
IOError: [Errno 2] No such file or directory: 'c:\\users\\brlu\\appdata\\local\\temp\\c6a13a_state.mallet.gz'
Please share any thoughts you might have.
Thanks.
This can happen for two reasons:
1. You have space in your mallet path.
2. There is no MALLET_HOME environment variable.
In my case I forgot to import gensim's mallet wrapper. The following code resolved the error.
import os
from gensim.models.wrappers import LdaMallet
os.environ['MALLET_HOME'] = 'C:/.../mallet-2.0.8/'
A more detailed explanation can be found here:
https://github.com/RaRe-Technologies/gensim/issues/2137
Make sure that mallet properly works from command-line.
Look to your folder 'c:\users\brlu\appdata\local\temp\...' if there are some files, you can deduce at which step mallet-wrapper fails. Try this step at command line.
I had similar problems with gensim + MALLET on Windows:
Make sure that MALLET_HOME is set
Escape slashes when set mallet_path in Python
mallet_path = 'c:\\mallet-2.0.7\\bin\\mallet'
LDA_model = gensim.models.LdaMallet(mallet_path, ...
Also, it might be useful to modify line 142 in Python\Lib\site-packages\gensim\models\ldamallet.py: change --token-regex '\S+' to --token-regex \"\S+\"
Hope it helps
Try the following
import tempfile
tempfile.tempdir='some_other_non_system_temp_directory'

How to download a file using Python

I tried to download something from the Internet using Python, I am using urllib.retriever from the urllib module but I just can't get it work. I would like to be able to save the downloaded file to a location of my choice.
If someone could explain to me how to do it with clear examples, that would be VERY appreciated.
I suggest using urllib2 like so:
source = urllib2.urlopen("http://someUrl.com/somePage.html").read()
open("/path/to/someFile", "wb").write(source)
You could even shorten it to (although, you wouldnt want to shorten it if you plan to enclose each individual call in a try - except):
open("/path/to/someFile", "wb").write(urllib2.urlopen("http://someUrl.com/somePage.html").read())
You can also use the urllib:
source = urllib.request.urlopen(("full_url")).read()
and then use what chown used above:
open("/path/to/someFile", "wb").write(source)

Categories

Resources