How do I change the import location of the modules in python?

Asked

Viewed 39 times

-2

import pygame

O programa da esse erro, dizendo que não tem nenhum modulo The program gives this error, saying that there is no module

Só funciona quando mostro o local para o programa: It only works when I show the location for the show

I’m starting my language studies. My python and vscode are installed on disk D:, I’m not understanding why modules are being imported on disk C:. I would like to change the location of the import, so I don’t keep showing the location of the archive every time

  • how many python versions are installed ?

  • Two, one that I installed on disk D: and one that was installed by the Microsoft Store app on disk C:

  • may have happened that the installations are linked to the installation in C and not to D. You have to specify which version will be used in the IDE. as vscode can create mv to manage the version in use. as this path, this for the version you want to use ?

  • vscode options. File Preferences Settings .. chooses which python interpreter to use.. only search for "python" and "Default Interpreter Path Path to Python, you can use a custom version of Python by modifying this Setting to include the full path."

  • I managed to solve my problem. Thank you very much

  • In the next questions, avoid putting code or data as images. This prevents searching the site, prevents people from copying and pasting snippets of your code as an example, and disrupts a lot more.

Show 1 more comment

1 answer

2

You have to learn about "virtual Nvironments": they are a resource used by the language for each project to have its own libraries, without any system conflict issues.

So, if you have a game that depends on pygame 1.8, and another that depends on pygame 2.0, you can have both installed on PC.

Also, using virtualenvs, all project dependencies are automatically found.

In general, more advanced Ides and editors like vscode support virtualenv - but of course, it’s a mechanism that doesn’t depend on Ides - only the language, and can be activated via terminal, or specified in a shortcut to run your program, create a link for it to run straight.

What a virtualenv does is put copies and shortcuts to Python and the system libraries inside a folder - which we usually keep together with the project (but it can stay anywhere). Before working with the project, or running it, we "activate" virtualenv. Then, you install any dependency on your project directly within virtualenv (even if you already installed it in the system’s Python, install it again). Then, as you understand the projects, you specify this dependency in the configuration files of the project itself - so Python can automatically install the dependencies for anyone who will install your project.

To do it on the command line: open the computer, and go with cmd to your project folder (with the "cd" command). When you get there, type python -m venv env - Python will create an "env" folder with everything in it. Next, activate virtualenv: env\scripts\activate.bat on Windows or source env\bin\activate on Linux/Mac. There you can type pip install pygame that it will be installed.

Paa work with virtuaenv in vscode, has a guide here: https://code.visualstudio.com/docs/python/environments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.