Go back and forth between environments in a script (e.g., Jupyter Notebooks) - python-3.x

I'm very confused in regards to whether it's possible to switch between environments in one script. I'm currently working Jupyter Notebooks and realized I do need a package that is in a separate (non-root) environment and would like to be able to import/use it. I initially tried to just load it into my root env, but it has so many conflicts, it's not possible. So trying to switch env mid script- is this not possible without going through hoops and going through Anaconda prompt? I've looked at about 30 articles and/or stackoverflow pages and it seems like my only option is to add my environment to the default sys.path within Jupyter OR potentially create another environment that contains both environment packages. This suggestion was unclear...not sure how to run this and I don't want to run through a shell/prompt either.
These were good resources:
RealPython: On Virtual Envs
StackOverflow: In which conda env is Jupyter executing?
Any clarity I can get on the topic for someone who doesn't understand this well, would be appreciated (Please explain as if I'm a Level 0/Beginner).

Now I'm looking back on this old post, thought I'd answer it since it never got an answer. While it's extremely frustrating to realize that needed packages don't mesh together mid-project, for each significant project, a new environment should be created. I still have a general environment that I use for minor projects, but for situations like this, I use anaconda's cheat sheet to make sure that in the new environment I'm creating, packages will all sync together by specifying versions directly in the creation in the environment. Also, getting comfortable with creating/copying .yml files and loading them directly into conda have also been helpful.
conda env create --file envname.yml
Once that's done, if I'm using jupyter notebooks, making sure it's been established as a kernel has been important.
Of course, if you're using an IDE like pycharm, you can bypass the conda prompt (pycharm is still talking to conda), which is sometimes just easier if you're on a deadline rather than going back and forth between different software.

Related

Activate venv in vs code

I have been trying to activate a virtual environment using Python built-in module venv from VSCode, But it didn't work properly And I didn't receive any error message. However, and also If I use venv\Scripts\activate.bat command in terminal it doesn't work.
Are you correctly setting up the venv?
python3 -m venv env
Then in the below section of your vscode taskbar you will find
Then select your interpreter(env) to use:
There are three things that I would check:
Does your vs-code have a default virtual environment defined as a user setting? Is that the one your current workspace is using?
Have you moved the folder you are working in since creating a virtual environment? If so, you should edit your venv/bin/activate script so that it has the correct value for the VIRTUAL_ENV variable.
In your project, do you have a .vscode/settings.json file that is referring to the wrong location or a location which doesn't exist? Specifically thinking of the "python.defaultInterpreterPath" setting.
These are things that I came across today when I had a similar problem. Hopefully that helps someone else!
{Ctrl+shift+'}
this will open a new terminal and automatically activate your virtual environment, found this in vs code documentation for flask virtual environments.
I also tried venv\Scripts\activate.bat and it wasn't having it; however cant remember the issue I was having.
Hope this saves some one a lot of time.

Python was not found but can be installed

I have just installed python3.8 and sublime text editor. I am attempting to run the python build on sublime text but I am met with "Python was not found but can be installed" error.
Both python and sublime are installed on E:\
When opening cmd prompt I can change dir and am able to run py from there without an issue.
I'm assuming that my sublime is not pointing to the correct dir but don't know how to resolve this issue.
This isn't a Sublime issue, it's a Windows 10 issue. My Windows 10 boxes don't have this feature so I'm not sure how widely spread it is, but you may want to check out this (seemingly unrelated) question "Permission Denied" trying to run Python on Windows 10.
The general gist from this post is that these new stub redirectors are supposed to direct you into installing some missing applications from the Windows App store and that user-installed versions are supposed to take priority, but depending on how the PATH is modified the system might find the stub versions before your user installed versions.
The currently accepted answer mentions:
The second part of correcting it is to type "manage app execution aliases" into the Windows search prompt and disable the store versions of Python altogether.
It's possible that you'll only need to do the second part, but on my system I made both changes and everything is back to normal now.
In the comments, there are comments from Zooba which further indicate that this is the way to go:
(Microsoft employee and CPython core developer here) You definitely only need to do the second part. There have been a couple of bugs related to upgrading apps resetting aliases which will be fixed in the next stable update, so it should be a one-time fix by then. While you're getting Insiders updates you may need to do it a couple more times.
... And launching the Store is a new feature to help people install Python - if you've added it to PATH using the regular installer it should take precedence over the new redirector, but if not you've discovered above how to disable it.
i had the same problem, so i went to the microsoft store (windos 10) and simply installed "python 3.9" and problem was gone!
sorry for bad english btw

nltk.download() wont open a GUI to chose the list of downloads. It dont download data and the cursor blinks forever

I am trying to install nltk and download the nltk data. I am trying this on python 3.7.3 and my pip is up to date. my PC is windows 10 and given by the company.
My Installation of nltk is succesfull but it wont downlaod the data. It dont give me GUI to chose downloads nor finishes the download. The cursor keeps blinking forever.
I have tried this running it as a an admin, ran it through Jupyter Notebook. Never saw the GUI at all/
Who knows what's going on? Maybe the download window is hidden behind other windows; I've seen that a lot. Or maybe it really doesn't come up. Either way, if you can't find the window you can largely work around the problem by using the non-GUI form of the downloader:
nltk.download("book") will download all the resources you'll need while reading the book. I recommend you just run this one and move on to exploring the nltk.
nltk.download("all") will download everything in the download store. Probably overkill.
nltk.downlead(<name>) will download resource <name> (e.g., 'average_perceptron_tagger' for the tagger data, etc.) If you try to use a module and it's missing a resource, it will usually tell you what you need to download.
There are some other collective names including all-corpora, popular, and third-party, but the most useful ones are the above, I believe.
This has been resolved. I had to bypass web connection proxy server at my work. Instead, they added permissions for the http://files.pythonhosted.org and this worked fine.

Setting up development environment for OpenBTS

I want to make some little changes in OpenBTS code and use it. Currently I am following this process
Make some changes in code. ( Can't do testing of these changes at runtime)
Build the packages
Install the packages
Setup or Run OpenBTS
Test the behavior of OpenBTS to see that those changes are reflected or not.
If not working, goto step 1
This a quite hectic process, is there any smarter way to do it. Like OpenBTS is directly run from code, rather than packages installed on Ubuntu. If I make change in code, and they are directly reflected in my setup. How i can setup this dev environment.
This answer is a bit late, I have just started to work on this my self. I don't bother installing the packages each time. My cycle is more like this:
Build the packages
Setup/run the database scripts (init the databases)
Install the packages that I don't need to re-build
Run each package manually (from the open BTS folders), e.g. run ../Transceiver, ../sipauthserver, ../OpenBTS, ../OpenBTSCLI ...etc...
Then when I want to make a code change - I do:
Stop everything
Code change
Re-build (e.g. just openBTS)
Re-run everything as before.
I also scripted the startup / stop sequences to make this faster (open/run each app in new terminals)

Package Python 3 executable that does not require programming knowledge

I would like to send my Python3 script to my father-in-law and grandmother. Each has their own Windows machine, one is running Windows 7 and the other is running XP.
Not sure how to package it up for them to run on their respective machines. Is there such a method?
My script prompts, while in the IDE environment, for Keyword, path, filename. So there are some inputs, the user has to type in. Not sure if that will affect the portable script creation.
After reading through some responses here on StackOverFlow, I found py2exe does not work with Python 3.
Also Pytonw, suggested here as well, looks very complicated. I don't think either of my relative could carry out those steps.
Lastly CX-Freeze site I get ublock filters-Badware risks and a big warning window when I visit their website.
I've used cx-freeze to deploy python apps compiled to windows .exe files for us by computer novice users for several years and it has worked well. you will occasionally run into issues with dependencies you will have to take extra steps for (Datetime for example) but nothing that isn't surmountable. The easiest way to handle it is to install the folder on the computer yourself and create a desktop shortcut to it for the user. That keeps it simple for them. If you are not close to them you can always use a program like team viewer to gain access to their computer like remote desktop.

Resources