r/learnpython • • 8d ago

Virtual Environment and Python

I have started learning python and decided to use pycharm as an editor. I want to know anything which will help me in this journey.

My main question is => I don't get "virtual environment" thing with python. I cloned a repo and opened it in pycharm, it says "No python interpreter configured for <repo-name>" and then suggested to create virtual environment.

I want to hear things from experience devs.

thank you.

4 Upvotes

43 comments sorted by

View all comments

-1

u/OkCaterpillar1058 8d ago edited 8d ago

To activate a virtual environment you can do:

cd project

source venv/bin/activate

If you want to make your own virtual environment

python3 -m venv venv

Then whatever packages you install, are only installed in that virtual environment, not system wide. The reason you do this, is to avoid conflicting packages. One project may need A 1.0 while another needs A 1.1 on the same system.

In PyCharm to change the interpreter:

  1. Press Ctrl + Alt + S to open settings and then select Python | All Interpreters.

  2. Select the interpreter and click Edit Interpreter… on the toolbar.

  3. In the Interpreter Settings dialog, change the interpreter name in the Interpreter Name field or modify the target settings. Then click Save.

If I remember right, in PyCharm when you start a new project, it makes a virtual environment automatically. But I haven't used it in a long time

3

u/8dot30662386292pow2 8d ago

To activate a virtual environment you can do:

cd project

source venv/bin/activate

This only works if the venv already exists.

If you want to make your own virtual environment

Yes, you must make your own always.

python3 -m venv project 

Per your last example this should be

python3 -m venv venv

And the name is often .venv, not venv.

1

u/fiddle_n 8d ago

Not to mention that all the instructions are Unix-specific - a significant number of users, especially beginners, use Windows for Python