r/learnpython • u/[deleted] • Oct 09 '20
Fucked up by not learning how to properly set up environment
Hello,
I have used Python for about 3 years or so now. However, I have made a huge mistake....
I always just pip install libraries from the command-line without setting up or using a virtual environment. I have used virtual environments at work before (for ML, to access my school's GPUs, etc.), I just never bothered to use one for my python projects.
Well, now I am going to start trying to make Python applications that will actually be used in a work environment, so I want to do it right. I want to abide by best practices for this sort of thing. I want to start using environments the correct way. I was going to completely uninstall Python from my computer, and then reinstall it, however I do not know if this is the best way to start. All I know is that I have like 50 libraries installed on my system itself, and those need to go.
Any help with what I should do? Also don't want to lose any of my old programs, but I know that just uninstalling Python should not directly affect this. Thank you in advance!
41
u/infazz Oct 09 '20 edited Oct 10 '20
I see a lot of recommendations here about conda.. That's really overkill and could actually cause some problems in a business environment (depending on how their current infrastructure is).
My recommendation is simply to use PyCharm as your IDE. Out of the box, when you create a new project it will create and enable a virtual environment for you. From there you just 'pip install' using the PyCharm terminal like you would anyway.
Edit: PyCharm Community (the 100% free version of PyCharm) has this feature
You can most likely set VSCode up to do this as well if that's what you prefer.
3
Oct 10 '20
Oh shit, I did not know Pycharm did this. Thank you. I usually use vscode but I have also used Pycharm.
2
1
u/enjoytheshow Oct 10 '20
VS Code will not create a venv for you but it’ll find one already created in your working directory and you can set it to be the default py installation for that current workspace.
0
Oct 10 '20
[deleted]
0
Oct 10 '20
[deleted]
3
u/IWishIWereFishing Oct 10 '20
This... Might as well use the built-in tools. When you're done installing packages you're a pip freeze away from having a reproducible environment.
11
u/Macambira Oct 09 '20
I've just recently have started to make some "serious" projects in python, and I've found Pipenv(https://pypi.org/project/pipenv/) to be an interesting way of making isolated environments, and allows you to easily creating new ones, and you can use pyenv to create an environment in version you want. But I've got a bias that Pipenv is basically Ruby's Bundler in Python, and having a background in Ruby it is significant to me.
6
3
u/mavericsb Oct 10 '20
I have been using it as well, and really like it. The only thing that I dislike is it’s speed when installing packages etc. The ease of use outweighs the few minutes it can take to add packages.
3
u/Macambira Oct 10 '20
Yeah, it's probably the biggest problem I have with it. The time it takes to lock can be really slow depending on the size of the package.
17
u/vidoardes vidoardes Oct 10 '20
I've read through the comments and as expected you have had tons of recommendations to use docker, anaconda, poetry, get your IDE to do it for etc...
Don't do any of that. That's all step 2.
The first step is to learn what virtual environments are, and how to build and activate them using the built in tool: venv.
Before you decide what to use to manage your virtual environments, you need to understand what is happening under the hood and what it is doing. It is incredibly simple, and will give you a better understanding of what all those tools are doing for you.
It's like when people ask about learning git, the comments argue over wether gitkraken is better than fork or sourcetree, when the answer for a beginner is use the command line. If you jump to the tooling without understanding the fundamentals, you'll have no idea how to solve problems when things go wrong.
4
Oct 10 '20
And after leaning venv it's good to ask yourself why it isn't enough because it is.
2
u/vidoardes vidoardes Oct 10 '20
Exactly. I use pycharm which does some of the heavy lifting for you, but it still uses venv under the hood. It just saves the hassle of switching environments when working on multiple projects.
30
u/Yojihito Oct 09 '20
I prefer conda for virtual envs per project.
11
u/hblock44 Oct 09 '20
I would second Anaconda. Once you get the hang of it, virtual envs are a breeze either through the command prompt or through the GUI
5
5
u/Schrodingers_gato Oct 09 '20
4th for conda. I've tried other packages but conda is definitely the easiest imo
0
Oct 09 '20
[deleted]
0
Oct 09 '20
[deleted]
6
u/Tanmay1518 Oct 10 '20
Im not the person you asked, but personally I have 3 versions of Python installed at a time. 3.7 for my school projects, 3.8 for my personal projects and 3.9 for learning the new features.
3.7 & 3.8 are on PATH while 3.9 isn't.
0
Oct 10 '20
[deleted]
5
u/Tanmay1518 Oct 10 '20 edited Oct 10 '20
Um I don't know exactly what changes allow users to now have multiple python installations but I found using venvs with multiple python versions to be very easy.
I just use the command
py -3.X -m venv .venvFor any project I'm starting
Replace X with the middle number of whatever python version you're using. For example, if I want a venv with 3.8, I'll type py -3.8 -m venv .venv
2
u/Random_User_81 Oct 09 '20
What is the advantages of conda?
10
u/delta-y Oct 09 '20
I'll talk about the times when I've used both conda and venv.
I use conda in my data science projects and my ML job. It is great because I am constantly switching to different projects and directories to do different tasks and I activate the conda environment I need wherever I am. In data science, I create a lot of scripts to explore data on any whim, so it's really nice to not have to know the path of the environment's location, I can just run the
conda activatecommand anywhere.Conversely, when I went through Miguel Grinberg's MEGA Flask Tutorial, I followed his lead and used venv, which I realized was a good option when building an application, for example, because I wanted that virtual environment to stick with that specific project. When building the application, I wanted that environment to be present in the directory so it felt "closer".
In the end, I like conda more because it is more flexible. It could easily have been used when building an application, whereas I don't think venv would fit the needs of data science projects as well. Hope this helps!
7
u/Periwinkle_Lost Oct 09 '20
I use venv and I like it more than conda. Venv folder is usually visible in the navigator of my editor so I just right click and copy relative path to the ‘activate’ script and paste it into the terminal
4
u/Tanmay1518 Oct 10 '20
Same. Plus conda has problems with installing certain packages as well
2
u/Periwinkle_Lost Oct 10 '20
I am actually trying conda as my venv for my current project just to see what it’s like. Given how popular it is I feel compelled to learn how to use it.
2
u/Tanmay1518 Oct 10 '20
I used to use it for my school packages but then I realised that using venv was much much faster and easier as well.
You can use conda if you wish to, but I would suggest getting miniconda to have better performance compared to the full fledged Anaconda environment.
2
u/delta-y Oct 10 '20
This is true, this is definitely a limitation that it has over venv for sure. In DS and ML, it has most of what one would need, but I bet with apps there’s so much missing. This is a crucial part that I missed in my explanation above, thanks for covering it
2
u/Random_User_81 Oct 10 '20
Great info, thanks for taking the time to explain. I'm going to give it a look.
1
4
u/TopicStrong Oct 09 '20
This is my current go to when I'm not reaching for docker. https://github.com/pyenv/pyenv
1
u/idwpan Oct 10 '20
pyenv does not... Manage virtualenv
https://github.com/pyenv/pyenv#in-contrast-with-pythonbrew-and-pythonz-pyenv-does-not
2
4
Oct 09 '20 edited Oct 11 '20
Coming from a NodeJS background, where you have a package config per project, I felt that the python setup was a little odd. That's until I started using pipenv. Super easy to use, and you can define so many things individually per project too
4
u/tzujan Oct 09 '20
I was in a similar boat, and I am still not a fan of virtual environments. So I spent a good month getting familiar with Docker - Docker Deep Dive by Nigel Poulton is an excellent book for this. VS Code is fantastic with Docker containers. All you do is select a folder as your workspace, click the little green button in the bottom left corner (vs containers needs to be installed), and connect to a container. The search bar will pop up, type in python, and six python container options from bare-bones, to minconda and full anaconda options, will present themselves. I tend to build as I go by editing the docker file with a requirements.txt file. Then as I pip to install packages, I'll do a pip freeze to update the text file. That way, when I relaunch the workspace, it will build with all my packages.
Since you mentioned ML, the Jupyter Stack on the docker hub is phenomenal - not sure how I got along before it.
5
u/fergal-dude Oct 09 '20
u/fake823 I used pycharm for years and tried VSCode once and have never looked back. It’s great, simple, and light. I find it much easier to use, give it a try.
2
u/fake823 Oct 10 '20
Ok, that sounds really interesting! Especially the "light" part. PyCharm gets quite slow sometimes. :/
Thanks for the recommendation! 😊
0
Oct 10 '20
[deleted]
5
u/Tanmay1518 Oct 10 '20
Much faster loading times.
More plugins for every langauge.
Much cleaner and modern ui. (not that pycharm's ui is bad, just feels a little outdated)
2
u/fake823 Oct 10 '20
The much faster loading times sound pretty promising! PyCharm is really lagging from time to time on my PC. :/
Maybe it's time to give VS Code a try! 😊👍🏼 Thanks for the feedback.
3
u/fergal-dude Oct 10 '20
u/45MonkeysInASuit, I really liked Pycharm, I used if for years and worked on some pretty big (for me) projects, multi-page flask applications with DB backends and user login.
BUT for smaller porjects, all of Pycharms bells and whistles were more than I needed 95% of the time. I find the Pycharm debugger unnecessarily cumbersome to setup, things like that.
VSCode has a much more modern interface and I find I can fire it up and be coding pretty fast as opposed to being bogged down with all the setup required in Pycharm. Please no hate, I do like the program, but I appreciate the VSCode way of doing things. I usually create automations at work and VSCode allows me to get up and running and for one to three file projects, it's a great tool.
4
u/Street_Worth Oct 10 '20
You can manually struggle to do everything Pycharm already does for you. The advantage of VScode is when you use multiple languages so you can use 1 IDE for everything. Also VSCode has some nice webdev plugins. In general Pycharm is far easier and better if you just do Python though/
2
Oct 09 '20
Reinstalling Python doesn't affect your existing code. You can always point them to your virtual environment Python if you're on linux. And for Windows you just need to do your\venv\python.exe your_code
2
u/iiMoe Oct 09 '20
I have a sort of dumb question cuz i never had to solve a problem with virtual env, if i use one does it also isolate my folders n stuff cuz i like to make lots of folders inside each other and sort my python scripts so if i use a virtual env will it tc of that too so i only see folders relevant to the project residing in the env?
4
u/MarsupialMole Oct 09 '20
It's pretty much just about where pip installs stuff, and where python looks for stuff. Python searches the current working directory the same way inside and outside the virtualenv.
2
2
u/FloppyEggplant Oct 09 '20
I wanted to do the same thing as you did - creating clean projects that could be used at any time, and that if I for some reason wanted to update any package I would still be able to use my old programs. I've taken notes on what to do when to create a project and created a repo with them.
I actually setup an environment, documentation and testing all at once. I create a project in a few minutes with everything I would possibly need. Here is the github repo.
2
u/Dangle76 Oct 10 '20
I always include a dockerfile/dockercompose in my repos/projects that mounts my current directory (the project) as a volume. Virtual environments are nice but I feel a makefile to install any libraries and running that in a build container for testing/deployment makes things simpler in the long run, as then you can easily make sure whatever environment it’s going to run in it’s going to work in, instead of testing your scripts on your local environment, and running into problems when you port it to the new environment/system.
2
u/dogfish182 Oct 10 '20
I always install 2 things in global python on a new computer
pipenv - to folder based manage my venvs pipx - great tool for cli python packages, let’s you appear to install global tools, but transparently manages virtual environments so you don’t need to worry about it. pipx install ansible and off you go. pipx inject ansible boto3 To add a dependency to your executable after the fact.
I have never had venv misery with this approach.
2
Oct 10 '20 edited Oct 10 '20
All you gotta do is https://docs.python-guide.org/dev/virtualenvs/#lower-level-virtualenv .
pip install virtualenvvirtualenv py-venv2 -p py2 (for python 2.7)virtualenv py-venv3 -p py (for python 3.8.x)virtualenv py-env39 -p py39 (for python 3.9)
And to make your life easier: https://docs.python-guide.org/dev/virtualenvs/#virtualenvwrapper because It also places all your virtual environments in one place.
(I'm on Windows, so that's a bit different than the guide, which is for Linux)
3
u/Deezl-Vegas Oct 10 '20
Hi! A lot of people are going to recommend Python tools like Virtualenv or Pipenv, and those are a good starting point.
In the professional world, Docker is the best solution. It's the primo shit. Docker provides an isolated environment with its own simulated OS, file system, and kernel.
1
0
Oct 10 '20
Can second this. If I am doing something simple I will just use conda env to set something up. If it's a full project then it gets a docker container.
1
Oct 10 '20
Hi, I start learning python about 1 mo. Could you clearly explain it for the beginner, please?
When did I use PyCharm I worked with the virtual environment like PipEnv?
1
1
u/duffer_dev Oct 10 '20
Like you, i too was not used to using virtual env. However, for quite some time, it has become second nature to use them. My goto option, is virtualenvwrapper . It's a wrapper around the virtualenv. The initial setup make take a while to use. But once you are set, it is very easy. Unlike virtualenv, that creates a folder in the project itself , this has all the environments tucked away in one folder. to create a virtualenv all you need is
mkvirtualenv my_envto use it again
workon my_env
deactivateIf you are using Linux, One trick is to add this lines to the ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
sh /usr/bin/virtualenvwrapper.sh1
u/lolslim 🐍Telegram🤖 Oct 10 '20
Mt intention is not to thread jack, but since pycharm is being discussed ill ask, im coding python in atom IDE on windows, that also puts my files on my ubuntu file server, do I need to use pycharm instead?
1
u/jdbow75 Oct 11 '20
While engaging someone with similar questions to you a little while ago, I wrote an article exploring many tools for managing virtual environments, and detailing a bit of the concept itself.
If you don't feel like reading it, here is the summary: you really won't go wrong with a tool like venv, virtualenv, Poetry, Pipenv, or Conda. But use something, and use virtual environments, for the reasons you mentioned. Cluttering up your system just isn't worth it.
Did you use pip to install all the packages you have? Or did you use other methods, like your system's package manager? If you just used pip, then you could pip freeze > cruft.txt then pip uninstall -r cruft.txt -y or something similar. This would make a list of packages, and then remove them. Might want to review the cruft.txt list first, though, just to be sure.
0
u/dxbtousa Oct 10 '20
I am like you , I just pip install and go on my way.
Isnt it easier to run your script from command line when the libraries are generally installed? With my 1 Virtual env projects - i have such a difficult time to run the script due to activating BAT, etc...
2
u/twistdafterdark jdelgit Oct 10 '20
You can just add the commands to a script and run that.
Linux
#!/bin/sh source env/bin/activate python script.py deactivateI think in Windows you'd just have to remove the first line and the source command. You can add it to a powershell or bat file and just run that script everytime
0
Oct 09 '20
[deleted]
19
Oct 09 '20
I don't think I've ever written a single line of code without saying fuck. So yeah, looks right to me.
22
Oct 09 '20
[removed] — view removed comment
7
5
u/Psilodelic Oct 10 '20
Conda comes with a lot of bloat. I’ve found pyenv to be much cleaner.
4
0
Oct 09 '20
[deleted]
22
u/fake823 Oct 09 '20
The same is possible with PyCharm, just in case he already has that one installed.
Just set up a new project and PyCharm will suggest you to create a venv. You don't even have to reinstall your system wide Python installation.
0
Oct 09 '20
[deleted]
9
u/fake823 Oct 09 '20
I've started with PyCharm about a year ago and I sticked to it, because it's really enjoyable to work with! :) Haven't tried out VS Code yet. Also because I'm only into Python up to now. So there hasn't really been a reason to change.
PyCharm is free as well (although there is also a "professional" version of it) and there are as well a lot of plugins available for Markdown, HTML etc.
197
u/prettyoaktree Oct 09 '20
A virtual environment creates an isolated environment, so it should not inherit whatever libraries are globally installed. Try to create a virtual environment for each new project, which should keep it nice and isolated and won't affect anything that uses globally installed libraries.