-1

just wanted to set up my new MacBookPro M1. As I want to organize my MB this time, i want to start using virtualenv. So, what I've done so far:

  • installed brew
  • installed virtualenv
  • set up a dir, in there create my first env called sec_env
  • installed some packages for testing

Now I want to use my virtualenv:

  • I started it, source sec_env/dir/activate
  • AND now here we go, I want to code something in this env. So I start up my code-insiders and try to import the package i already installed....does not work ;( (EDIT1: Maybe i failed config it inside vs code?)

Do I missunderstand the use of virtualenv? I thought of it kinda like a virtual machine...So i can install package in need for one project and code it. But if i work on another, i would just switch, start up my vs-code again and keep writing on the other project.

Or is the problem just, that all the project I want to code have to be inside the dir of the virtualenv(sec_env)? At the moment , I have a dir virtualenvs where I store all my environments , start one up and change to desktop to work . And all the projects are on my desktop.

Would be awesome if someone give me any tipps on this, or another way to separate my different projects. I am super new to this topic, since I used different virtual-box images before...now i am forced to use something else...M1 :D !

  • "_So I start up my code-insiders and try to import the package i already installed_" what does that mean? What is "_code-insiders_"? – sinoroc Jan 12 '21 at 22:40
  • VS Code doesn't maintain your activated virtualenv when started. – OneCricketeer Jan 12 '21 at 22:42
  • @sinoroc i just mean the vs-code editor but the apple silicon version is called something like that. Yes, well i just create a new *.py file and start importing. – PR0M37H3US Jan 12 '21 at 22:44
  • @OneCricketeer ok, but is there a way to do that ? Can i work on two different projects totally independent ? Sorry, i am not that familiar with these topics...and as i said, I just used virtualbox before – PR0M37H3US Jan 12 '21 at 22:45
  • You need to look up how to couple visual studio code with a python virtual environment. That should be quite straightforward. There are many similar questions with good answers already. Is there something unique to your question that is not covered already? – sinoroc Jan 12 '21 at 22:45
  • Does this help? https://code.visualstudio.com/docs/python/environments#_where-the-extension-looks-for-environments – OneCricketeer Jan 12 '21 at 22:46
  • @sinoroc I think you should be right. So i will search for that and if its straightforward , I am hopefully able to set it up right :) Thanks PS. do i need to tag you or did you get noticed anyway,.....new user here – PR0M37H3US Jan 12 '21 at 22:51
  • @OneCricketeer yes, already read about it in my research... not super trivial for me to understand. But with these information i will do further research or hit up one of my tutors – PR0M37H3US Jan 12 '21 at 22:53
  • The gist from that link is that VSCode should _detect_ a venv in the project folder, but from my experience, it does not "inherit" the activated environment when you start it from the CLI – OneCricketeer Jan 12 '21 at 22:57
  • I got it , hopefully. But it looks like i now have to projects in my vs-code and one with the interpreter set to my main ...Resources/Python.app/Contents/MacOS/Python and my other project users .../environments/sec_env/bin/python and it seems like they work independently. Thanks ! @OneCricketeer – PR0M37H3US Jan 12 '21 at 23:10

1 Answers1

0

Your understanding is generally correct as virtualenv are a way to keep projects' dependencies separated from each other, like a VM would.

Your code doesn't need to be in the same directory as where your virtual environment, but many people tend to organize it that way for the sake of convenience. That way you don't need to think about what venv you coded a project with since it's right there in the directory.

With your steps, I think you installed a package before activating the environment. Doing it in that order installs the package in your system site-packages, not your virtual environment packages. Before you install a package, you need to activate your environment. Also, it appears from How to tell Homebrew to install inside virtualenv? that homebrew doesn't support installing a package into a virtual env. So in order to install packages into a virtualenv, I would suggest using pip as your package manager.

So the sequence of commands would be...
source <path to virtualenv>/dir/activate
pip install <modules you want to install>
# Now you can run your code that references those installed modules. 
  • 1
    Thanks! I think I messed up the description in my english...sorry. I should installed everything correctly, since i just tried ipyhton inside it and there i was able to import the packages :) I think @sinoroc just pointed out correctly its just a wrong setup in vs-code... going to search for that – PR0M37H3US Jan 12 '21 at 22:48